Template method

Started by
8 comments, last by iMalc 18 years, 9 months ago
I have the following code:

class Scope: public BaseClass {
	public:
		template <class T>
		T * GetData();
};

	RestoreHandle::pList = pScope->GetData<HandleList>(); // <= Error
However, I get the following compilation error: "error C2275: 'HandleList' : illegal use of this type as an expression" If I put int instead of HandleList, I get the error: "error C2062: type 'int' unexpected" Why I get errors?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Advertisement
I'm guessing it's because pScope is of type BaseClass * and BaseClass contains a non-template member function GetData. So you're trying to provide a template parameter to a non-template function. Are you trying to override a virtual function with a template function? If so, it can't be done.

Enigma
class BaseClass {	public:		virtual ~BaseClass();};

pScope is of type Scope *
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Maybe visual C++ 6 doesnt support such tempates?
I really need help with this, my work is stuck because of this.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
MSVC 6 does not support templated member functions.
Quote:Original post by SiCrane
MSVC 6 does not support templated member functions.


It does, it just doesn't support defining them outside of the class definition. The following compiles just fine in MSVC 6.0

template <typename TT>class Test{public:	template <typename T>	void test(T t)	{		i = t;	}private:	TT i;};int main(void){	Test<int> t;	t.test(0);	return 0;}

--Michael Fawcett
Quote:Original post by mfawcett
Quote:Original post by SiCrane
MSVC 6 does not support templated member functions.


It does, it just doesn't support defining them outside of the class definition. The following compiles just fine in MSVC 6.0

*** Source Snippet Removed ***

No it doesnt.
I have tried that as well.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower.Quote:Original post by Toolmaker Quote:Original post by The C modest godHow is my improoved signature?It sucks, just like you.
Quote:Original post by The C modest god
Quote:Original post by mfawcett
Quote:Original post by SiCrane
MSVC 6 does not support templated member functions.


It does, it just doesn't support defining them outside of the class definition. The following compiles just fine in MSVC 6.0

*** Source Snippet Removed ***

No it doesnt.
I have tried that as well.


I tried it before I posted. I just tried it again. It compiles fine.
--Michael Fawcett
The problem with MSVC++ 6 is both that the template definition needs to appear inside of the class definition and also that it can't handle member function templates whose template parameters are explicitly specified. In other words, the difference between mfawcett's example and your example is that his template arguments are deduced by the function argument whereas in yours, you are specifying the template argument.

The only way around this in MSVC++ 6 is to make your function take a dummy parameter whose purpose is to help the compiler deduce the template arguments:

template< typename TemplateArgument >struct template_argument{};class Scope: public BaseClass {	public:		template <class T>		T * GetData( template_argument< T > dummy ) { return new T; }};int main(){  Scope blah;  int* a = blah.GetData( template_argument< int >() );  delete a;}
Just use out parameters instead of return values in MSVC6:
class Scope: public BaseClass {	public:		template <class T>		GetData(T *& t);};pScope->GetData(RestoreHandle::pList);
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

This topic is closed to new replies.

Advertisement