Jump to content
  • Advertisement
Sign in to follow this  
choffstein

Template...issues

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

So I am designing an object factory in my game, and decided to write a nice little test program. (You can skip this code. It was only posted for reference to my later issue...though its not where the issue lies.)
class Object
{
	public:
		Object(){}
		virtual ~Object(){}
};

class A : public Object
{
	public:
		A(){}
		~A(){ std::cout << "Deleting " << s.c_str() << endl; }
		void initialize(std::string _s){ s = _s; }
		std::string returnString(){ return s; }
	private:
		std::string s;
};

std::vector<Object *> Objects;

template<class T> T *createClass()
{
	T *t = new T;
	Objects.push_back(t);
	return t;
}

void releaseAll()
{
	std::vector<Object *>::iterator it = Objects.begin();
	for(it; it != Objects.end(); it++)
	{
		delete *it;
		//Objects.erase(it);
	}
	Objects.clear();
}

int main()
{
	A *a = createClass<A>();
	a->initialize("a");
	std::cout << a->returnString().c_str() << endl;
	releaseAll();
	return 1;
}
Anyway, it works PERFECTLY. That is, until I try to put it in my engine. Then I start getting some wierd errors. Example:

//create global in CObjectFactory.h
class CObjectFactory
{
	...
	public:
		template<class Type> Type *create();
        ...
};
//create global
CObjectFactory g_ObjectFactory


//CObjectFactory.cpp
template<class Type> Type *CObjectFactory::create()
{
	Type *t = new Type;
	Objects.push_back(t);
	return t;
}

//CObject.h
class CObject
{
	public:
		CObject(){}
		virtual ~CObject(); //make sure child's deconstructor is called
		virtual std::string toString();
		virtual void initialize();
};

//CTerrain.h
class CTerrain : public CObject
{
   ...
};


//in main method
Terrain *t = g_ObjectFactory.create<CTerrain>(); //error here
BAM! Compiler ERROR! It says:
Quote:
: error C2275: 'CTerrain' : illegal use of this type as an expression : see declaration of 'CTerrain' : error C2059: syntax error : ')'
This doesn't make sense at all to me. Does anyone know whats up? Thanks! [Edited by - visage on March 3, 2005 9:32:30 PM]

Share this post


Link to post
Share on other sites
Advertisement
Which compiler are you using? I believe that VC++ 6 has problems with using templates like that.

After cleaning up about a dozen problems in your code, I got it to compile with VS .NET 2003.

Share this post


Link to post
Share on other sites
Here is the code I got to compile:
#include <iostream>
#include <vector>

using namespace std;

//Object.h
class Object
{
public:
Object(){}
virtual ~Object(); //make sure child's deconstructor is called
virtual std::string toString();
virtual void initialize();
};

//CTerrain.h
class CTerrain : public Object
{
};




class A : public Object
{
public:
A(){}
~A(){ std::cout << "Deleting " << s.c_str() << endl; }
void initialize(std::string _s){ s = _s; }
std::string returnString(){ return s; }
private:
std::string s;
};

std::vector<Object *> Objects;

template<class T> T *createClass()
{
T *t = new T;
Objects.push_back(t);
return t;
}

void releaseAll()
{
std::vector<Object *>::iterator it = Objects.begin();
for(it; it != Objects.end(); it++)
{
delete *it;
//Objects.erase(it);
}
Objects.clear();
}

//create global in CObjectFactory.h
class CObjectFactory
{
public:
template<class Type> Type *create();
};
//create global
CObjectFactory g_ObjectFactory;


//CObjectFactory.cpp
template<class Type>
Type *CObjectFactory::create()
{
Type *t = new Type;
Objects.push_back(t);
return t;
}

int _tmain(int argc, _TCHAR* argv[])
{
CTerrain *t = g_ObjectFactory.create<CTerrain>();
return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
BAM! Compiler ERROR! It says:
Quote:

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


This doesn't make sense at all to me. Does anyone know whats up?
Thanks!

Not yet looking over all of the code, but let me guess -- you are using VC++ 6.0, correct? 6.0 chokes on member function templates whose template arguments are explicit. So, if you in fact are using 6.0, either upgrade (preferable if possible), or make the creation function a non-member function, or make your creation function have a dummy parameter from which the template argument can be yielded.

An example of the latter solution:


template< typename Type >
class dummy
{
};

class a
{
public:
template< typename Type >
void function( dummy< Type > )
{
}
};

int main()
{
a blah;
blah.function( dummy< int >() );
// function should be callable in VC++ 6.0
}


Edit: Ha, figure VC++ 6.0

Quote:
Original post by visage
VC++ 6 compiles the first one just fine...
What are these "dozens" of problems that you speak of?


Name pretty much anything even remotely complex (and some things not-so-remotely complex) with templating, and VC++ 6.0 can't handle it. It's an old compiler (came out in 1998 I believe, prior to the standard being finalized) with two newer versions since its release which are much more compliant. You really should consider upgrading if you can.

Share this post


Link to post
Share on other sites
Yeah, it should mike. That was just a typo in the post, not my code. But thanks for pointing it out.

John -- do you see any major changes? That looks just like what I have in my code already...

Thanks so far guys...

Share this post


Link to post
Share on other sites
Quote:
Original post by visage
...
John -- do you see any major changes? That looks just like what I have in my code already...


There are a bunch of typos. Two were already pointed out. Another is "Object" vs "CObject". But I think the real problem is VC++ 6. As I said (and Polymorphic OOP restated), VC++ 6 has problems compiling some usages of templates.

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!