user2228675
user2228675

Reputation: 71

Expected initializer before '<' token

I have a template class myClass prototyped in a header file, and am implementing it in a .cpp file that is included at the end of the header file. When I use the code:

template<typename T>
class myClass {
public:
    void myFunction(const T item);
};

in the header file and

template <class T>
void myClass<T>::myFunction(const T item)
{
//stuff
}

in the implementation file, I get the above error on line 2 of the implementation code. I have used this same exact syntax in another program with successful compilation and correctly functioning results, so I am quite confused. There are three different function definitions in the .cpp file and all have this same error on their respective lines. I assume I am making a small error but I really can't seem to figure it out.

Help and explanation are very much appreciated.

EDIT:

Here is an SSCCE which has the same error: main.cpp

#include <iostream>
#include "myClass.h"

using namespace std;

int main(){
myClass<int> example;
example.myFunction(1);
return 0;
}

myClass.h

#include<iostream>

#ifndef MYCLASS_H_
#define MYCLASS_H_

template<typename T>
class myClass {
public:
    void myFunction(const T item);
};

#include "myClass.cpp"
#endif /* MYCLASS_H_ */

myClass.cpp

using namespace std;

template <class T>
void myClass<T>::myFunction(const T item)
{
    cout << "Hello World!";
}

and I am using Code::Blocks 10.05 with the GNU GCC compiler.

Upvotes: 2

Views: 15729

Answers (5)

troore
troore

Reputation: 797

Keep implementation in myClass.cpp file is also possible. Apart from the implementation itself, you need to put declarations with specific types of T. For example, if T could be int and float, myClass.cpp should be like:

using namespace std;

template <class T>
void myClass<T>::myFunction(const T item)
{
    cout << "Hello World!";
}

// Declarations with specific types.
template void myClass<int>::myFunction(const int item);
template void myClass<float>::myFunction(const float item);

Upvotes: 0

user2228675
user2228675

Reputation: 71

I finally solved the problem by removing the myClass.cpp from the build configuration. Not sure why this was necessary, but it works perfectly now.

Upvotes: 3

Vivek Jain
Vivek Jain

Reputation: 3889

Templates are instantiated on demand and given the complexities which would be induced in compiler design most of the compilers build a restriction to keep the template declaration and definition in one single file. If not so, template declaration on top of class and function definitions would create a pattern difficult for the compiler to detect a pattern and instantiate the template.

Similar question has been discussed on Stackoverflow, and explained on Parashift and cplusplus.com (Read: Templates and multiple-file projects at the end of the article).

Hope this helps!

Vivek

Upvotes: 0

bash.d
bash.d

Reputation: 13207

It is very difficult to declare a template in an .h file and define it in a .cpp file. The compiler needs template declaration and definition in a single file in order to create the code. So if your compiler does not support export keyword it won't work. So you should not use different files for declaration and definition. See also this thread.

Upvotes: 0

masoud
masoud

Reputation: 56479

Put the implementation of the method in the header file (.h) too

The compiler needs to know details of implementation in translation unit.

Upvotes: 1

Related Questions