Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

PorkBag

Class Templates

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

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?

Share this post


Link to post
Share on other sites
Advertisement

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 :/

Share this post


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

Share this post


Link to post
Share on other sites

  • 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!