Template problem with visual studio.net

Discussion in 'C++' started by Vespasian, May 22, 2004.

  1. Vespasian

    Vespasian Guest

    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
     
    Vespasian, May 22, 2004
    #1
    1. Advertising

  2. Vespasian

    Leor Zolman Guest

    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
     
    Leor Zolman, May 22, 2004
    #2
    1. Advertising

  3. Vespasian

    David Harmon Guest

    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/
     
    David Harmon, May 22, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wh
    Replies:
    2
    Views:
    603
    Cowboy \(Gregory A. Beamer\)
    Jan 16, 2004
  2. Thirumalai
    Replies:
    0
    Views:
    762
    Thirumalai
    May 22, 2006
  3. rockdale
    Replies:
    1
    Views:
    702
    Juan T. Llibre
    Aug 23, 2006
  4. xman
    Replies:
    0
    Views:
    583
  5. xman
    Replies:
    1
    Views:
    666
    Victor Bazarov
    Aug 17, 2005
Loading...

Share This Page