Jump to content
  • Advertisement

Archived

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

MajorShredd

Templated Class Producing Linker Error

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

Hello, I''m having trouble with a C++ templated class. Using MSVC++ 6, I get linker error LNK2001 saying that the function calls are unresolved external symbols. I''m almost sure my problem stems from syntax somehow. I''ve tried "Rebuild All" for the project and I still get linker errors for every member function I call in main(). If it makes any difference, these are being called currently from a WM_CREATE handler in Win32. My code is pasted below. MyVector.h
#ifndef __CUSTUTIL_MYVECTORH
#define __CUSTUTIL_MYVECTORH
////////////////////////////////////////////////////////////////


template < class T >
class MyVector{

	public:
		//constructors destructors
		MyVector(void);
		~MyVector(void);
                ...

		//operators
		T& operator[](int nPosition);
                ...

};
////////////////////////////////////////////////////////////////
#endif //defined __CUSTUTIL_MYVECTORH 
MyVector.cpp
#include "MyVector.h"
#ifndef NULL
  #define NULL 0
#endif
////////////////////////////////////////////////////////////////
template < class T >
MyVector::MyVector(void)
	: listSize(0), ptrList(NULL)
{
	//no extra inits
}
////////////////////////////////////////////////////////////////
template < class T >
MyVector::~MyVector(void){
	if(list) delete [] list;
}
////////////////////////////////////////////////////////////////
template < class T >
T& MyVector::MyVector[](int nPosition)
{
        //incomplete
	if(nPosition >= listSize) return NULL;
	else if(nPosition < 0) return NULL;
	else return list[nPosition];
} 
_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.

Share this post


Link to post
Share on other sites
Advertisement
To expand on what she 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
My partner had the same error the other day. She must have had some funny invisible character in her file or something as when she copied each function into a new file and used that instead all was well.

Share this post


Link to post
Share on other sites
Thanks, those links and advice did the trick

_________________
:: MajorShredd ::
The glass is neither half full nor half empty;
rather, it is a combination of both, and the system is perfect.


[edited by - MajorShredd on June 4, 2004 7:01:26 PM]

Share this post


Link to post
Share on other sites
Hi!

I have the same problem.
Think pre-compiled header files may solve this problem (I''m not sure)
In fact, I tried to use PCH but it didn''t work. So far, I don''t know how to create and use PCH files.

So please show me the way (how to create and use PCH files)!
Thanks alots.

Share this post


Link to post
Share on other sites
Hi

I want to create a static lib (a linked-list lib). But when I use templates it doesn''t work. The linker raises some errors (like above).

So can we create a static lib and use templates (in this lib)?
if can, please show me the way!

Thanks

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!