473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
Jul 22 '05 #1
6 2283
On 10 Feb 2004 18:48:35 -0800 in comp.lang.c++, sy*******@yahoo.com
(Shen) was alleged to have written:
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

Jul 22 '05 #2
"Shen" <sy*******@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
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

Jul 22 '05 #3

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in:
files. Last I header, microsoft had no intention ever to implement


Ahem .... last I 'heard'

Jonathan
Jul 22 '05 #4
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

Shen wrote:
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

Jul 22 '05 #5

"Shen" <sy*******@yahoo.com> wrote in message
news:81**************************@posting.google.c om...
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
Jul 22 '05 #6
>
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...

Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Long | last post by:
Problem: to insert the content of a file in an HTML document at a specific location. One possible way is to add a WebCharm tag like this: <%@charm:text 20 0 my_include_file.txt %> When the...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
11
by: Duncan Winn | last post by:
I would like to use CStringArray, but when I try to include afxcol.h my compiler (VS.NET) complains that... WINDOWS.H already included. MFC apps must not #include <windows.h> Any...
4
by: Anders Eriksson | last post by:
Hello! I'm using VC++ 7.1 and MFC. In a header file that is located in a different directory that the main project I include a header file that is located in the main project directory. The...
3
by: Arpi Jakab | last post by:
I have a main project that depends on projects A and B. The main project's additional include directories list is: ...\ProjectA\Dist\Include ...\ProjectB\Dist\Include Each of the include...
2
by: Susan Baker | last post by:
Hi, I am (trying) to compile some code I downloaded from the internet. The sources contain references to header files - using the form : #include <pathname/file> If I change the form to...
14
by: Jon Rea | last post by:
I am currently cleaning up an application which was origainlly hashed together with speed of coding in mind and therefore contains quite a few "hacky" shortcuts. As part of this "revamping"...
2
by: --== Alain ==-- | last post by:
Hi, Under VC++.NET, i have a stupid issue with #include statement. Usually when we include some *.h file, we need to declare them before any #using <or #using namespace... to avoid issue at...
15
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what controls to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.