Jump to content
  • Advertisement
Sign in to follow this  
TimChan

WIERD template

This topic is 5003 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

oops, this is my problem
#include <stdio.h>
#include <STDLIB.H>

class A {
public:
	A() {}
};

class CMaker {
public:
	CMaker(){}

	template <typename _T>
	_T* Make() {
		return new _T;
	}
};

int main() {
	CMaker maker;
	A *a = maker.Make<A>();

	return 0;
}



error C2275: 'A' : illegal use of this type as an expression see declaration of 'A' error C2059: syntax error : ')' how to solve it?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by TimChan
oops, this is my problem

*** Source Snippet Removed ***

error C2275: 'A' : illegal use of this type as an expression
see declaration of 'A'
error C2059: syntax error : ')'

how to solve it?


It compiles with vs.net so I guess this is a VC6 problem (don't forget to state it). I played around with it and it seems to be a limitation of the poorly implemented VC6 template management code (well, not poorly implemented but remember VC6 shipped in 1998 - the same year as the C++ standard).

Sorry, I don't know how to solve this - oh, maybe you can use another compiler? If you can't afford vs.net, then you can try to use devcpp.

Regards,

Share this post


Link to post
Share on other sites

template <class _T>
class CMaker {
public:
CMaker(){}

template <typename _T>
_T* Make() {
return new _T;
}
};







Notice the extra "template <class _T>" line before the "class CMaker" line.

This tells the compiler that in this class, every to exchange every instance of "_T" with the type that you are using.

So, the code should work now. But keep in mind, I'm not sure if you can use an underscore like this. You might have to change every _T to just T, but see how it goes.

Share this post


Link to post
Share on other sites
oh i forgot to state, sorry. is there any other problems inside VC6?
well, devcpp slows down the development i think.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endar
*** Source Snippet Removed ***

Notice the extra "template <class _T>" line before the "class CMaker" line.

This tells the compiler that in this class, every to exchange every instance of "_T" with the type that you are using.

So, the code should work now. But keep in mind, I'm not sure if you can use an underscore like this. You might have to change every _T to just T, but see how it goes.


1) No, the code still produce the same error - and you now add a very weird thing: what _T will the function use when it is going to be instancied?

2) using the underscode like this is not forbidden, but the C++ standard says that all identifier which begins with a underscore may be reserved for compiler use. Moreover, it is better to write template arguments in a way which is readable too (after all, they are just like function arguments. You don't name your function arguments like that, isn't it?). I usually use DESCRIPTIVENAMES for template arguments.

3) This completely change the philosophy of templated members. With the previous code you are able to write:


Maker maker;
A * maker.make<A>();
B * maker.make<B>();


With your code, you just can't.

Quote:
Original post by TimChan
well, devcpp slows down the development i think.

4) Some may not find it as easy to use as VC6 (debug may be harder). The code editor is very good, there is little bugs here and here (I use the dev version, not the stable one) but nothing critical.

Share this post


Link to post
Share on other sites
Just a tip, having #include <STDLIB.H> rather than #include <stdlib.h> may not compile on some compilers (GCC is case sensitive for include files, but AFAIK VC6 isn't).

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!