tags:

views:

73

answers:

4

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.

A: 

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

template <typename T>
T test<T>::out() 
{ 
} 
Stewart
The OP didn't indent the code, so the `<typename T>` was taken as an unknown HTML element and ignored.
Marcelo Cantos
Whoops - good point, although I think the answer is still right - I just got the wrong missing argument list.
Stewart
@Marcelo Cantos - changed. Thanks for the feedback
Stewart
Thanks a lot every one for the immediate answers!!! :D :D
Isuru
A: 

This line is wrong:

test<T>::test();

Just write this:

test();
Marcelo Cantos
This is wrong; that would just define a new free function instead of the class method.
tzaman
I guess I should have been clearer. I'm referring to the declaration in the class, not the definition after. (The semicolon should give it away.)
Marcelo Cantos
Oh, didn't see that. -(-1) :)
tzaman
+5  A: 

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

Dewfy
A: 

You need to declare in front of every class method, so:

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

Builds fine for me after that change.

tzaman