Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

Template method

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}



Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!