Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How to define a member function outside of its template?

0
Posted

How to define a member function outside of its template?

0

It is not ok to provide the definition of a member function of a template class like this: // This might be in a header file: template class xyz { void foo(); }; // … // This might be later on in the header file: void xyz::foo() { // generic definition of foo() } This is wrong for a few reasons. So is this: void xyz::foo() { // generic definition of foo() } The proper definition needs the template keyword and the same template arguments that the class template’s definition was declared with. So that gives: template void xyz::foo() { // generic definition of foo() } Note that there are other types of template directives, such as member templates, etc., and each takes on their own form. What’s important is to know which you have so you know how to write each flavor. This is so especially since the error messages of some compilers may not be clear as to what is wrong. And of course, get a good and up to date book. If you have a nested member temp

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123