473,729 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template linker error

Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<s ome_user_define d_type> goo;
}
now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defin ed_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template< user_defined_ty pe>::~some_temp late<user_defin ed_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding?

Sep 26 '05 #1
3 1657
Gr*****@nospam. com wrote:
Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<s ome_user_define d_type> goo;
}
now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defin ed_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template< user_defined_ty pe>::~some_temp late<user_defin ed_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding?


Likely what's wrong is that you specified that there would be a
destructor for some_template<> but didn't actually supply the code for
it. The same would happen with a non-template class:

class A { ~A(); };
int main() { A a; return 0; } // Error: no A::~A()

If it's not that, then post a compilable sample of code that
demonstrates the problem.

Cheers! --M

Sep 26 '05 #2
Gr*****@nospam. com wrote:
Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<s ome_user_define d_type> goo;
}
now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defin ed_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.


I guess that is an unwarranted assumption. Sure the compiler is going to
generate code for some_template<s ome_user_define d_type> and sure that
code is going to be in the object file, but I don't think there is any
requirement that code be usable at link time by other object files
(which I think it what you are talking about).

You might try placing this

template class some_template<s ome_user_define d_type>;

at file scope in main.cpp. It's called an explicit instantiation and
maybe it will achieve what you want.

Personally I just put template code in header files.

john
Sep 26 '05 #3

Gr*****@nospam. com wrote:
Hi,

I have a question that pertains to Templates and link time. This is
totally for my own understanding and to correct what's obviously an
erroneous view of things on my behalf;

Lets say I have;

// main.cpp

template <classT>
class some_template
{

... details

};

int main(int argc, char** argv)
{
some_template<s ome_user_define d_type> goo;
}
now when I compile main.cpp the compiler is going to see that it needs
to generate an instance of some_template for some_user_defin ed_type".
It will then go ahead and compile up this generated code into a .obj
which can later be used at link time.

How can it be at link time I can get a link error for
"some_template< user_defined_ty pe>::~some_temp late<user_defin ed_type> "
? I mean the compiler has already generated and compiled it up into the
object code and that is to be used by the linker. Assuming the linker
is using the correct .obj etc. what's missing that link errors can
arise like that? I would have thought that everything was generated by
the compiler. Or rather what's wrong with my understanding? From your example, it appears that the template's implementation

resides in a source (.cpp) file.

Templates should be completely implemented in header files. The
compiler needs to have seen a template definition before it compiles
any source files that use the template. Doing so ensures that all the
templates are instantiated by the time that the program is linked. So
making sure that all template code resides in header files could well
fix your problem.

Greg

Sep 26 '05 #4

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

Similar topics

2
3985
by: Ruben Campos | last post by:
I have a problem with a template function that is declared as a friend of a template class. I'll first show the exact problem with source code: // MyClass.hpp template <typename T> class MyClass { // ... friend void MyFriendFunction (MyClass <T> * const ptrMyClass); // ...
5
1786
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
2
9401
by: haplotype | last post by:
I have designed a package with several files, which is listed bellow base.cpp & base.hpp - define the template class base tree.cpp & tree.hpp - define the class tree derived from base<int> client.cpp & client.hpp - define the class client derived from tree main.cpp - the main function instantiate client and run All these files are compiled successfully, but there's a linker error " undefined reference to `base<int>::base_run()' ". ...
0
1955
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that the resulting functions or classes are identical, without causing any errors. Or so I thought. But a few days ago, I added a couple of new template functions to one of my personal library modules, and I got this linker error message (from...
3
2669
by: little.freaky | last post by:
Hello group, I have a problem with template classes and inheritance. I've searched on the internet to find a solution but all the examples look the same as my code (as far as I can tell) and I can't find my mistake (maybe there's something wrong with my eyes). I've two classes: template< class T >
3
3758
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
5
2536
by: Wayne Shu | last post by:
Hi, guys I am reading Vandevoorde and Josuttis 's "C++ Template The Complete Guide" these days. When I read the chapter 15: Traits and Policy classes. I copy the code in 15.2.2 that use to determining the class type. The code is below:
3
2122
by: michael | last post by:
Hi All, Not sure if this is a c++ issue or a compiler issue. I have the following in a class: template<class T> bool Utility::getInput(T& i, T low, T up){ bool result = false; if(cin >i){
5
2380
by: 2beagles | last post by:
I have a template for a three dimensional array that I have been working on using Visual C++ 6.0. Under version 6 this code worked fine. However, last night I downloaded Visual C++ 2005 Express and and trying to get the same code to work. Under Visual C++ 2005 I am seeing linker errors. The problem is in the definition of a couple of friend operators that the linker does not like for some reason. I am assuming I have a mistake in my...
3
1603
by: Boltar | last post by:
Hi I have a template class thats defined in a header file and implemented in a .cpp module that is compiled to a .o file. No problems there. But when I try to use this class from another .o file I get undefined error messages from the linux linker, eg: cl_foo<int>(...) not defined. Presumbly this is because theres no actual instance of cl_foo<int> created in the .o file that contains the template class. Is there a
0
8925
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
8763
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8156
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
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6026
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4797
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3240
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
2
2692
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.