In the following code, I get a compile error that I don’t have if I remove the templates :
template<int DIM> class myClass
{
public :
enum Mode {aa,bb};
myClass(){};
};
template<int DIM> class myClass2
{
myClass2(){};
void myfunc(myClass::Mode m);
};
template<int DIM>
void myClass2<DIM>::myfunc(myClass<DIM>::Mode m)
{
}
test.cpp(19) : warning C4346: ‘myClass::Mode’ : dependent name is not a type
prefix with ‘typename’ to indicate a type
test.cpp(19) : error C2146: syntax error : missing ‘)’ before identifier ‘m’
If I remove the like:
template<int DIM>
void myClass2<DIM>::myfunc(myClass::Mode m)
I get :
test.cpp(19) : error C2955: ‘myClass’ : use of class template requires template argument list
And if I put the definition of myfunc directly in the declaration of the class (which I would like to avoid), it works.
What should I do and why does this happen?
Thanks
I believe that you have two problems in your code. The first is in this declaration in
myClass2:Because
myClassis a template, you need to specify what the template parameter is. I assume that you probably meant to writeHowever, due to a weird idiosyncrasy in C++, you would write this as
The
typenamekeyword here tells C++ thatModeis the name of a type nested inside of the classmyClass<DIM>.Similarly, later in the code, the code
should read
to tell the compiler that
Modeis the name of a type.Hope this helps!