Thread: template implementation... bad syntax?

  1. #1
    coder
    Join Date
    Feb 2008
    Posts
    127

    template implementation... bad syntax?

    hi again
    here is a trouble that I used to skip... now that came the time to solve it, I couldn't find the solution:

    avl_tree.h
    Code:
    #ifndef AVL_TREE_H
    #define AVL_TREE_H
    
    template <class T>
    class avl {
    	public:
    		avl ();
    		~avl ();
    };
    
    #ENDIF
    avl_tree.cpp
    Code:
    #include "avl_tree.h"
    
    template <class T> avl<T>::avl() {}
    
    template <class T> avl<T>::~avl() {}
    My "main.cpp" program will #include "avl_tree.h".
    If I compile "main.cpp" and "avl.cpp" and link them together, it's all ok.
    But if I main.cpp contains an instantiation
    Code:
    avl<int> tree;
    I get from the linker:
    g++ -O0 -g3 -o test main.o avl_tree.o
    main.o: In function `__tcf_1':/home/.../src/main.cpp:11: undefined reference to `avl<int>::~avl()'
    main.o: In function `__static_initialization_and_destruction_0':/home/...main.cpp:11: undefined reference to `avl<int>::

    what am i missing?
    Last edited by carlorfeo; 03-02-2008 at 07:04 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    coder
    Join Date
    Feb 2008
    Posts
    127
    thanks laserlight
    I should have read that page better when you already posted it before
    After reading I wanted to know how the STL has been implemented:
    Is the whole code into headers, or do they use other solutions? Those weird headers have made me a bit confused.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Is the whole code into headers
    The ones I have looked through have their template code implemented entirely in headers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Replies: 11
    Last Post: 11-13-2002, 01:29 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM