简体   繁体   中英

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()
{
}

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

Your definition of the out member is missing the template argument list. out should read:-

template <typename T>
T test<T>::out() 
{ 
} 

This line is wrong:

test<T>::test();

Just write this:

test();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM