Skip to main content
gamedev.net
🔒 Locked

Class Templates

Started by PorkBag 2 replies 750+ views
Original Post
PorkBag
PorkBag
In an attempt to use class templates I''ve created a simple class, which of course doesn''t work.
template <class Type>
class Box
{
	Type Width;
	Type Length;
public:
	Box ()
	{
		Width = 25;
		Length = 30;
	};
	Type GetArea ();
};

template <class Type>
Type Box<Type>::GetArea ()
{
	return Width*Height;
};

Box<int> Square;
When i try using Square.GetArea() in the code i get the following error: "unresolved external symbol "public: int __thiscall Box::GetArea(void)" (?GetArea@?$Box@H@@QAEHXZ)" THe .h and .cpp files in which this is located are properly linked (other things in them work). Any idea what''s wrong?
dathui
dathui

template <class Type>
class Box
{
Type Width;
Type Length;
public:
Box ()
{
Width = 25;
Length = 30;
};
Type GetArea ();
};

template <class Type>
Type Box<Type>::GetArea ()
{
return Width*Height;
};

Box<int> Square;


tried removing the ; after the ctor? othervise i cant find anything wrong :/
reality is only an option
SiCrane
SiCrane
To expand on what he said: unless your compiler supports the export keyword, and I''m willing to bet that yours doesn''t, then you can''t put the definition of a template in a separate source file. The complete definition of the template needs to be available at point of instantiation, which means, in effect, that the definition needs to go into the header. (Or an inline file of some sort, etc.)

For more details see these articles: "Export" Restrictions, Part 1 and "Export" Restrictions, Part 2.

And since petewood no longer posts on the boards, I suppose I should post this link for him. It''s an article entitled "Instantiator: A (Mostly) Portable Framework for Separate Compilation of Templates"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.