Problem in writing template function inside class
-
Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "
error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'
". Anybody knows whats wrong with the code?class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; }
Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like..class Test { public: template<class T> void someFunction( T obj )// no error { } };
nave [OpenedFileFinder]
-
Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "
error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'
". Anybody knows whats wrong with the code?class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; }
Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like..class Test { public: template<class T> void someFunction( T obj )// no error { } };
nave [OpenedFileFinder]
It should be:
template <class T>
class Test
{
void function(T);
};
Maxwell Chen
modified on Friday, December 28, 2007 4:42:31 AM
-
It should be:
template <class T>
class Test
{
void function(T);
};
Maxwell Chen
modified on Friday, December 28, 2007 4:42:31 AM
No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.
nave [OpenedFileFinder]
-
No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.
nave [OpenedFileFinder]
Naveen, It's a bug of Microsoft Visual C++ (6.0) compiler. There's no workaround available. They have corrected it in the newer versions. Please check KB article[^]
-Sarath. "Great hopes make everything great possible" - Benjamin Franklin
My blog - Sharing My Thoughts, An Article - Understanding Statepattern
-
Hi all, I have a template function inside a class a shown below. But while compiling I am getting an error "
error C2893: Failed to specialize function template 'void __thiscall Test::someFunction(T)' With the following template arguments: 'int'
". Anybody knows whats wrong with the code?class Test { public: template<class T> void someFunction( T obj ); }; template<class T> void Test::someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; Test obj; obj.someFunction( n );// error comes here return 0; }
Also if I wrote the implementation of the template function inside the class itself, then there is no error. Like..class Test { public: template<class T> void someFunction( T obj )// no error { } };
nave [OpenedFileFinder]
Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you.
template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
-
No no thats not possible. I only need to call the function with different types. I think the problem is with the vc6. In vc8, it is working fine.
nave [OpenedFileFinder]
Oh I misunderstood your question. Sorry!
Maxwell Chen
-
Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you.
template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
Hi Mahesh , The problem with your sugeestion is that, I cannot call the obj.someFunction() with both int and long at the same time like..
int n = 0; long l = 0; obj.someFunction( n ); obj.someFunction( l );
nave [OpenedFileFinder]
-
Hi Naveen , You are right the code given by you is working on VC7 and onwards But if you want to make it possible on VC6 then please refere the code given below and make the changes accordingly. Hope this will help you.
template<class T> class Test { public: void someFunction( T obj ); }; template<class T> void **Test<T>::**someFunction( T obj ) { } int main(int argc, char* argv[]) { int n = 0; **Test<int>** obj; obj.someFunction( n );// error comes here return 0; }
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
I think that Naveen wants (member)
function templates
of a class, notclass templates
. If it is a class template, the argument type is fixed when the class object is declared. So we can not invoke member functions with different input types (something like function overloading).
Maxwell Chen
-
I think that Naveen wants (member)
function templates
of a class, notclass templates
. If it is a class template, the argument type is fixed when the class object is declared. So we can not invoke member functions with different input types (something like function overloading).
Maxwell Chen
HI Naveen and MaxWell I am really sorry ..It was my misunderstanding. later I worked for wht Naveen exactly want, on VC6 But it was not working then i googled, I found the Following link http://forums.devx.com/archive/index.php/t-82949.html[^]
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
-
HI Naveen and MaxWell I am really sorry ..It was my misunderstanding. later I worked for wht Naveen exactly want, on VC6 But it was not working then i googled, I found the Following link http://forums.devx.com/archive/index.php/t-82949.html[^]
The secret of life is not enjoyment but education through experience. - Swami Vivekananda.
To tell the truth, I have been 7 years not coding template stuffs. I just tried hard to recall everything about templates (both member function template and class template), and tried with VC++2008. It works well finally. ;P
Maxwell Chen