Thread: Linker error for an undefined reference to a constructor

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    17

    Question Linker error for an undefined reference to a constructor

    Code:
     Error: [Linker error] undefined reference to `Node<QA>::Node(QA)'
    Im writing a basic program for an animal guessing game (similar to twenty questions) using a binary search tree. The project consists of a lot of header files, so things are bound to get messy. But this is one error I haven't been able to figure out.

    Here's the function I've narrowed the problem down to:

    Code:
        bool QATree::insert(string o, bool q)
        {
           Node<QA>* newnode = new Node<QA>(QA(o,q)); // this constructor function call causes the error.
           if (!root)
           {
              root = newnode;
              return true;
           }
           if (insert(root, newnode));  
              return true;    
        }
    ...And the seperate .cpp file for the Node class (it's friended to QATree).

    Code:
        #include "bsttools.h"
        template <typename T>
        Node<T>::Node(T s)
        {
            data = s;
            left = NULL;
            right = NULL;
        }
    If you have any questions, or can't see the error here and would like to see more code, I'll be glad to help you help me.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    All templated code must be placed in the header files that are included in all the various source files that use the templated class. You cannot split them, the templated class, up into seperate header and source files like you would do with non-templated classes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You can split them, but only if you #include the .cpp file in the .h file.

    Actually there are other ways too, but this is most common.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by King Mir
    You can split them, but only if you #include the .cpp file in the .h file.

    Actually there are other ways too, but this is most common.
    You REALLY should not get in the habit of including source files!
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by hk_mp5kpdw
    You REALLY should not get in the habit of including source files!
    You can call it an .hpp file if you want. The idea is to create symetry with classes, and sepetate the implementation from the class declaration.

    Having a teplate is a sourse file only makes the compiler create an empty .o file. No real harm done.
    Last edited by King Mir; 10-12-2006 at 05:00 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    17
    Thank you very much! I had doubted that anyone could understand my problem, because even my computer science teacher was stumped. I'll definately come back to this forum next time I need help.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    You and your teacher may want to become familiar with C++ FAQ LITE, starting here.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. Getting undefined reference error from ZThread
    By 6tr6tr in forum C++ Programming
    Replies: 3
    Last Post: 04-21-2008, 05:50 PM
  3. Gnarly Linking Error HELP!!!!
    By brooksbp in forum C++ Programming
    Replies: 8
    Last Post: 05-04-2007, 01:00 AM
  4. Compiling Tutorial program with Dev-c++
    By h3ro in forum C++ Programming
    Replies: 15
    Last Post: 10-24-2006, 03:02 AM
  5. nehe opengl prob
    By bluehead in forum C++ Programming
    Replies: 6
    Last Post: 03-29-2005, 12:01 AM