linker error when using template class in Dev-C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haplotype
    New Member
    • Sep 2005
    • 1

    linker error when using template class in Dev-C++

    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 " [Linker error] undefined reference to `base<int>::bas e_run()' ".

    However, I have defined this function in base.cpp, and all .cpp files are added to the project. I use IDE Dev-C++ 4.9.9.2. I don't know why the linker cannot find this function. When I get ride of the template, then problem solved. But I really need the class "base" to be a template class. When I use the template but melt all these files into one file, problem also solved. But I don't like that.

    I have made the program to be very small (doing nothing, only shows the problems) and attached it with this note in the file problem_example .zip.

    Can anybody tell me why this happen? what's wrong with template class?

    thanks a lot
    Attached Files
  • wcm4Him
    New Member
    • Dec 2007
    • 1

    #2
    Originally posted by haplotype
    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 " [Linker error] undefined reference to `base<int>::bas e_run()' ".

    However, I have defined this function in base.cpp, and all .cpp files are added to the project. I use IDE Dev-C++ 4.9.9.2. I don't know why the linker cannot find this function. When I get ride of the template, then problem solved. But I really need the class "base" to be a template class. When I use the template but melt all these files into one file, problem also solved. But I don't like that.

    I have made the program to be very small (doing nothing, only shows the problems) and attached it with this note in the file problem_example .zip.

    Can anybody tell me why this happen? what's wrong with template class?

    thanks a lot

    I too have a very similar problem at the moment and I too am using the Dev C++ IDE as well, while searching the web for a solution, I found this FAQ about this problem. Read 35.12 through 35.15.

    haplotype I hope that link helps you.

    Although it did solve the problem, I don't really like the solutions it gives (not to mention the first & third (export keyword) solutions don't even work with the Dev C++ Complier). So my question is, is there another solution to this problem with templates or am I just gonna have to use the solutions the FAQ gave?

    Comment

    • Prasannaa
      New Member
      • May 2007
      • 21

      #3
      As of now, compilers requires the template definitions in the h file itself, if it is used in multiple translation units.So you need to move the template functions to the h file. That should get rid of the link error. You can declare/define the template functions in the cpp file, if you are going to use those functions only with in that translation unit.

      Prasannaa

      Comment

      Working...