template implementation... bad syntax?

This is a discussion on template implementation... bad syntax? within the C++ Programming forums, part of the General Programming Boards category; hi again here is a trouble that I used to skip... now that came the time to solve it, I ...

  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 06:04 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,340
    >> 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, 12:29 PM
  5. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21