Advertisement

C++ templates... *argl*

Started by December 14, 2003 09:36 AM
13 comments, last by Corrail 21 years, 2 months ago
Hi all! I just wanted to code a template class and got the following problem: Writing this in a header file work without problems:

template<class T>
class VectorArray
{
public:
	T& operator [] (unsigned int index)
	{
		return T();
	}
};
But writing this in a header file

template<class T>
class VectorArray
{
public:
	T& operator [] (unsigned int index);
};
And this in a cpp file

template<class T>
T& VectorArray<T>::operator [] (unsigned int index);
{
	return T();
}
will give me a linking error if I call operator []: unresolved external symbol "public: int & __thiscall VectorArray::operator[](unsigned int)" I''m using MS Visual Studio .NET 2002. Thanks for every help -------------------------------------------------------- "If it looks good, it is good computer graphics" "If it looks like computer graphics, it is bad computer graphics" Corrail corrail@gmx.at ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Just off the top of my head, you have a '';'' in the cpp file where it doesn''t need to be.

Change:
T& VectorArray::operator [] (unsigned int index);

to:
T& VectorArray::operator [] (unsigned int index)

and that should help.
Dreams arn't just dreams, They're a whole new world to play in.
Advertisement
Sorry my fault:

In my cpp file there is (without ; ):

template<class T>T& VectorArray<T>::operator [] (unsigned int index){    return T();}


--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081

[edited by - Corrail on December 14, 2003 12:49:12 PM]
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
You can''t seperate declaration from definition with templates. Write the full implementation in the header.
Oh, didn''t know that!
Thanks a lot!

--------------------------------------------------------

"If it looks good, it is good computer graphics"
"If it looks like computer graphics, it is bad computer graphics"

Corrail
corrail@gmx.at
ICQ#59184081
--------------------------------------------------------There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable.There is another theory which states that this has already happened...
Yup, what he said... I had the same problem writing all my templated functions, I had to move all code into the .h file and it all works fine now.

--- Edit ---
Just wanted to mention, I beleive other compilers allow you to do the .h/.cpp with templates without issue, but I use vc 6.0, and it only works with the implementation in the header. I beleive in .NET this is fixed, but since I don't use it, no promises that it's fixed .

[edited by - Ready4Dis on December 14, 2003 12:08:44 PM]
Advertisement
quote:
Original post by Ready4Dis
Just wanted to mention, I beleive other compilers allow you to do the .h/.cpp with templates without issue, but I use vc 6.0, and it only works with the implementation in the header. I beleive in .NET this is fixed, but since I don''t use it, no promises that it''s fixed .


IIRC, the export keyword should allow you to seperate prototypes and implementation like that, but there are very few compilers that support it. Certainly no version of VC++ or GCC does.
Yeah, but it''s crappy too to have to write all the code in the header. One solution is to write the implementation in a .INL file and include it in the header (which is a little more intuitive than writing it in a CPP file and excluding it from the build, and certainly nicer than writing everything in one file, even if it requires an all-dependency rebuild after any modification).

Later,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

As a side note, VS.NET (2002) still has the issue.

[twitter]warrenm[/twitter]

quote:
Original post by sbennett
IIRC, the <code>export</code> keyword should allow you to seperate prototypes and implementation like that, but there are very few compilers that support it. Certainly no version of VC++ or GCC does.


The current GCC supports separate linking of template implementations in cpp files, although I''m not exactly sure how to activate that behaviour. It also comes with some negative side effects.

This topic is closed to new replies.

Advertisement