c++ - Template error with dependent names -
in following code, compile error don't have if remove 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 not type
prefix 'typename' indicate type
test.cpp(19) : error c2146: syntax error : missing ')' before identifier 'm'
if remove like:
template<int dim> void myclass2<dim>::myfunc(myclass::mode m)
i :
test.cpp(19) : error c2955: 'myclass' : use of class template requires template argument list
and if put definition of myfunc
directly in declaration of class (which avoid), works.
what should , why happen?
thanks
i believe have 2 problems in code. first in declaration in myclass2
:
void myfunc(myclass::mode m);
because myclass
template, need specify template parameter is. assume meant write
void myfunc(myclass<dim>::mode m);
however, due weird idiosyncrasy in c++, write as
void myfunc(typename myclass<dim>::mode m);
the typename
keyword here tells c++ mode
name of type nested inside of class myclass<dim>
.
similarly, later in code, code
template<int dim> void myclass2<dim>::myfunc(myclass<dim>::mode m) { }
should read
template<int dim> void myclass2<dim>::myfunc(typename myclass<dim>::mode m) { }
to tell compiler mode
name of type.
hope helps!
Comments
Post a Comment