You must include the .cpp file instead of only .h file when using templates in VC++ ...?

S

Shen

I saw this conclusion in a couple of groups. For example, if you
define a class template, in VC++ you have to merge the .h and .cpp
files into one file and include it in other files where they have
something to do with this template class. Otherwise you'll get Link
errors(unresolved external symbol...LNK2001/LNK2019).

Is it still true now? Has MS Visual studio 2003 done something for it?
or does anyone figure out a solution other than changing the .h/.cpp
style?

It is not good to include implementation in other files, for it tends
to put redefinition bugs into your project and...anyway it is off the
..h/.cpp way.


Thanks for the reply,


Shen
 
D

David Harmon

I saw this conclusion in a couple of groups. For example, if you
define a class template, in VC++ you have to merge the .h and .cpp
files into one file and include it in other files where they have
something to do with this template class.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[34.12] Why can't I separate the definition of my templates class from
it's declaration and put it inside a .cpp file?" It is always good to
check the FAQ before posting. You can get the FAQ at:
http://www.parashift.com/c++-faq-lite/

Please don't invite an off-topic thread by crossposting to comp.lang.c++
and product-specific groups. Pick the _one_ most relevant group to post
to. See the welcome message posted twice per week in comp.lang.c++ or
available at http://www.slack.net/~shiva/welcome.txt
 
J

Jonathan Turkanis

Shen said:
I saw this conclusion in a couple of groups. For example, if you
define a class template, in VC++ you have to merge the .h and .cpp
files into one file and include it in other files where they have
something to do with this template class. Otherwise you'll get Link
errors(unresolved external symbol...LNK2001/LNK2019).

Is it still true now? Has MS Visual studio 2003 done something for it?
or does anyone figure out a solution other than changing the .h/.cpp
style?

It is not good to include implementation in other files, for it tends
to put redefinition bugs into your project and...anyway it is off the
.h/.cpp way.

There is a language feature called export which allows you include
function template and class template static data definitions in .cpp
files. Last I header, microsoft had no intention ever to implement
this feature.

I have never used export, because of its limited availability, but I
think you can do something like this:

------------------------------
"my_template.hpp":
------------------------------

#ifdef HAS_EXPORT
#define EXPORT export
#else
#define EXPORT
#endif

EXPORT
template<typename T>
struct my_template {
void f();
static int* g;
};

#ifndef HAS_EXPORT
#include "my_template.cpp"
#endif

------------------------------
"my_template.cpp"
------------------------------

#ifndef HAS_EXPORT
#include "my_template.h"
#endif

template<typename T>
void my_template<T>::f() { /* */ }

template<typename T>
int* my_template<T>::g;

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

This allows you to take advanage of export where it is available.
(Conceivably there could be subtle difference in the meanings of
programs depending on whether HAS_EXPORT is defined, because of
name-lookup rules.)

Jonathan
 
C

Carl Daniel [VC++ MVP]

Review the thread on this very newsgroup (microsoft.public.vc.stl) titled
"vc++ 2004" for an elaborate discussion of the value and costs of export,
which is the C++ language feature that lets you keep template
implementations separate from template definitions. To date, Comeau is the
only compiler vendor shipping an export-capable compiler, so MS is far from
alone in not supporting it.

-cd
 
J

John Harrison

Shen said:
I saw this conclusion in a couple of groups. For example, if you
define a class template, in VC++ you have to merge the .h and .cpp
files into one file and include it in other files where they have
something to do with this template class. Otherwise you'll get Link
errors(unresolved external symbol...LNK2001/LNK2019).

Is it still true now? Has MS Visual studio 2003 done something for it?
or does anyone figure out a solution other than changing the .h/.cpp
style?

It is not good to include implementation in other files, for it tends
to put redefinition bugs into your project and...anyway it is off the
.h/.cpp way.


Thanks for the reply,


Shen

Simplest option is to put the template function directly in the header file,
just like an inline function.

john
 
P

Patrik Stellmann

Simplest option is to put the template function directly in the header file,
just like an inline function.

Another quite common way with same effect is to put the inline and
template functions in a .inl-file and include that file at the bottom of
your header. Keeps your header file 'clean' from implementations...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top