PDA

View Full Version : Template and Visual C++


GFalcon
09-14-2005, 12:28 PM
Hi everybody,

I have a little problem with templates under visual c++ (.NET), actually I try to implement a templated method in a templated class:

template <typename T>
class MyClass
{
template<typename T2>
T2& MyMethod();
};

I want to define the method, in an other file, this way:

template <typename T>
template <typename T2>
T2& MyClass<T>::MyMethod()
{
... method implementation
}

It works fine with gcc compiler but does not with visual.

What am i doing wrong ?

anubis
09-14-2005, 12:46 PM
Are you implementing the methods as inline ? It won't work if you put the methods into a .cpp file (at least not if you don't exclude the file from compilation).

GFalcon
09-14-2005, 12:51 PM
It is not inlined, the method is in fact defined in another file that is included at the end of my header file that declares the class.

bramz
09-14-2005, 01:12 PM
My guess is that you're using .NET 2002 (the 7.0). you can't define templated methods out of line in that one (although it's perfectly legal in C++). You can either upgrade to .NET 2003 or define it in the class definition, like this:


template <typename T>
class MyClass
{
template<typename T2>
T2& MyMethod()
{
... method implementation
}
};


I hope this helps,
Bramz

GFalcon
09-14-2005, 01:17 PM
My guess is that you're using .NET 2002 (the 7.0). you can't define templated methods out of line in that one (although it's perfectly legal in C++).


So that's why :)
I'll just have to upgrade my compiler then

Thanks for the answer