Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template problem with visual studio.net

Reply
Thread Tools

Template problem with visual studio.net

 
 
Vespasian
Guest
Posts: n/a
 
      05-22-2004
I am getting the follwoing errors,
1. temp error LNK2019: unresolved external symbol "public:
__thiscall Spice<int>::Spice<int>(void)" (??0?$Spice@H@@QAE@XZ)
referenced in function _main
2. temp fatal error LNK1120: 1 unresolved externals

when I compile the follwing code

#MY HEADER FILE

template<class X>
class Spice {
public:
Spice();
private:
};

# CPP FILE #1
#include <iostream>
#include "temp.h"
using namespace std;

template<class X>
Spice<X>::Spice()
{
sdafdsafsda;
}

# CPP FILE #2
#include <iostream>
#include <string>
#include "temp.h"
using namespace std;



int main(void) {
Spice<int> a;
return 0;
}
\

The compiler doesn't catch the 'sdafdsafsda' error in CPP FILE #1. Any
help is appreciated,

TIA,
ves
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      05-22-2004
On Sat, 22 May 2004 02:16:27 GMT, Vespasian <>
wrote:

>I am getting the follwoing errors,
> 1. temp error LNK2019: unresolved external symbol "public:
>__thiscall Spice<int>::Spice<int>(void)" (??0?$Spice@H@@QAE@XZ)
>referenced in function _main
> 2. temp fatal error LNK1120: 1 unresolved externals
>
>when I compile the follwing code
>
>#MY HEADER FILE
>
>template<class X>
>class Spice {
>public:
> Spice();
>private:
>};
>
># CPP FILE #1
>#include <iostream>
>#include "temp.h"
>using namespace std;
>
>template<class X>
>Spice<X>::Spice()
>{
> sdafdsafsda;
>}
>
># CPP FILE #2
>#include <iostream>
>#include <string>
>#include "temp.h"
>using namespace std;
>
>
>
>int main(void) {
> Spice<int> a;
> return 0;
>}
>\
>
>The compiler doesn't catch the 'sdafdsafsda' error in CPP FILE #1. Any
>help is appreciated,


Remember that in the inclusion model (the way most platforms work),
templates that aren't instantiated in the current translation unit are
checked syntactically but not fully compiled...and then discarded if
they're never instantiated in that TU. Your implementation of that
constructor in FILE #1 is never instantiated in that translation unit, so
the compiler doesn't care that the identifier sdaf(etc) is undefined, and
no constructor makes it into the object file.

When compiling FILE #2, on the other hand, the compiler doesn't see the
information from FILE #1, and so it cannot instantiate the constructor
template. Finally, the linker exposes the problem.

If you do it this way, you get the syntax error you're looking for:

#include <iostream>
#include "temp.h"
using namespace std;

template<class X>
Spice<X>::Spice()
{
sdafdsafsda;
}

int main(void) {
Spice<int> a;
return 0;
}

Moral of the story: put templates into header files if they're going to be
needed by more than one TU.
-leor



>
>TIA,
>ves


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      05-22-2004
On Sat, 22 May 2004 02:16:27 GMT in comp.lang.c++, Vespasian
<> wrote,
> 1. temp error LNK2019: unresolved external symbol "public:
>__thiscall Spice<int>::Spice<int>(void)" (??0?$Spice@H@@QAE@XZ)
>referenced in function _main
> 2. temp fatal error LNK1120: 1 unresolved externals


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
r H2 deduce deduce template argument of a template class inheritingfrom a non template base? nguillot C++ 5 03-08-2009 05:56 PM
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
template template arguments: expected a class template, got `Component<T1, T2, T3> gary.bernstein@gmail.com C++ 1 06-08-2007 07:10 AM
Re: A Newbie Question about template template template tom_usenet C++ 0 07-24-2003 12:06 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57