Question regarding C++ Templates
I used a simple class for a test program about templates, this is what I did:
template <typename T>
class test
{
public:
test<T>::test();
T out();
};
template <typename T>
test<T>::test()
{
}
T test<T>::out()开发者_JAVA技巧
{
}
int main()
{
//test<int> t;
}
But when I try to compile it says 'T' : undeclared identifier and use of class template requires template argument list , pointing to the same line, where I have implemented the method out() . Can anyone please explain what the problem is?? I'm using visual studio 2008.
Following is more accurate:
template <typename T>
class test
{
public:
test();
T out();
};
template <typename T>
test<T>::test()
{
}
template <typename T>
T test<T>::out()
{
}
1) Don't use <T>
inside class
2) Don't forget to declare template <T>
before each method declaration out of class body
This line is wrong:
test<T>::test();
Just write this:
test();
Your definition of the out member is missing the template argument list. out should read:-
template <typename T>
T test<T>::out()
{
}
精彩评论