473,320 Members | 1,930 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,320 software developers and data experts.

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<some_user_defined_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_defined_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_type>::~some_template< user_defined_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 1644
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<some_user_defined_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_defined_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_type>::~some_template< user_defined_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<some_user_defined_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_defined_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<some_user_defined_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<some_user_defined_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<some_user_defined_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_defined_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_type>::~some_template< user_defined_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
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...
5
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
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>...
0
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...
3
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...
3
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...
5
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...
3
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
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...
3
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.