Archived

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

C++ templates... *argl*

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

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can''t seperate declaration from definition with templates. Write the full implementation in the header.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
To be more precise, I pretty sure there is only one compiler/linker that actually implements the export keyword.

And even the seperation of the .h and .cpp files does not yield what you might expect. It is not a true build once and rebuild later when there are changes. It just takes the template cpp and generates an itermediate file that it can use later when compiling other source files. And from an artical I read, it even increases build time.

I forget the name of it, but i think it is the brand that the VC6 (among others) compiler/linker was built off of.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You can still write your function implementations in a .cpp or .tempate or whatever file, all you need yo do is #include them before you #endif in your header file.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
You can still write your function implementations in a .cpp or .tempate or whatever file, all you need yo do is #include them before you #endif in your header file.


Yeah, in the Almighty Boost Library, they use .ipp files for this purpose.

Share this post


Link to post
Share on other sites