Templated Class Producing Linker Error

Started by
5 comments, last by MajorShredd 19 years, 10 months ago
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.
Advertisement
Try putting everything from your .cpp into your .h and deleting your .cpp.

-fel
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
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"
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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]
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.
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

This topic is closed to new replies.

Advertisement