473,626 Members | 3,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(unresolv ed external symbol...LNK200 1/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 2298
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*******@yaho o.com> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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(unresolv ed external symbol...LNK200 1/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.hp p":
------------------------------

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

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

#ifndef HAS_EXPORT
#include "my_template.cp p"
#endif

------------------------------
"my_template.cp p"
------------------------------

#ifndef HAS_EXPORT
#include "my_templat e.h"
#endif

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

template<typena me 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******@kanga roologic.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.publ ic.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(unresolv ed external symbol...LNK200 1/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*******@yaho o.com> wrote in message
news:81******** *************** ***@posting.goo gle.com...
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(unresolv ed external symbol...LNK200 1/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
2894
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 HTML template is processed by a WebCharm-aware web server, the content of my_include_file.txt is inserted at the tag location. This work very much like the SSI #include tag. However, you have more control
5
2505
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 very carefully define the order in which paths are searched with command line options on the compiler. Both can cause problems, especially when dealing with complex software distributions. It occurs ot me that by extending the C include...
11
10130
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 suggestions???
4
5822
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 project is called SCLaser. When I right-click on the #include "SCLaserDoc.h" and select Open Document SCLaserDoc.h, it can't find the file and lists all the directories that I
3
2697
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 directories contain a file named "cppfile1.h". In my main project I #include "cppfile1.h". I rely on the order of paths in additional include directories list to get file cppfile1.h from ProjectA and
2
3003
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 this: #include "pathname/file"
14
6689
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" process I am introducing namespaces to properly compartmentalise sections of the code into logical units. What I am speciffically trying to get right is the dependency tree for header files to reduce compile time and simplify the code structure. On...
2
305
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 compilation time. However, when i write #include "commctrl.h" as below in my *.h file : #pragma once #include "commctrl.h"
15
5263
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 show - text boxes, input boxes, buttons, hyperlinks ie the usual. The data is not obtained directly from a database.
0
8265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8637
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7193
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2625
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1808
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.