Thread: Class/Template...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Class/Template...

    I must be overlooking something here, but I get the following errors
    Code:
    :C:\C++Projects\test\main.cpp:9: undefined reference to `X_Container<char>::X_Container()'
    :C:\C++Projects\test\main.cpp:9: undefined reference to `X_Container<char>::~X_Container()'
    :: === Build finished: 2 errors, 0 warnings ===
    The class declaration looks like this
    Code:
    #ifndef CLASS_H
    #define CLASS_H
    /*************/
    template<class Param>
    class X_Container
    {
    public:
        X_Container();
        X_Container(int Size);
        ~X_Container();
    
        void Insert(Param type, int Pos);
        void Remove(Param type, int Pos);
        void Clear();
    
        long Size();
        bool Resize(int size);
        bool AddSize(int amount);
    
    private:
        Param* type;
        int size_;
    
    };
    
    /*************/
    #endif
    The implementation is as follows, and I have look up and down this, and cant find the problem.
    Code:
    #include "class.h"
    /****************/
    
    template<class Param>
    X_Container<Param>::X_Container()
    {
        //Default small size
        size_ = 1000;
        //Dynamically allocate memory for the type
        type = new Param[size_];
    }
    
    template<class Param>
    X_Container<Param>::X_Container(int Size)
    {
        //User Selected Size
        size_ = Size;
        //Dynamically allocate memory for the type
        type = new Param[size_];
    }
    
    template<class Param>
    X_Container<Param>::~X_Container()
    {
        //Set all pointers to null
        for ( int iter = 0; iter != size_; ++iter )
        {
            type[iter] = 0;
        }
        delete []type;
    }
    
    template<class Param>
    void X_Container<Param>::Insert(Param type, int Pos)
    {
        //
    }
    
    template<class Param>
    void X_Container<Param>::Remove(Param type, int Pos)
    {
        //
    }
    
    template<class Param>
    void X_Container<Param>::Clear()
    {
        //
    }
    
    template<class Param>
    long X_Container<Param>::Size()
    {
        return size_;
    }
    
    template<class Param>
    bool X_Container<Param>::Resize(int size)
    {
        //
    }
    
    template<class Param>
    bool X_Container<Param>::AddSize(int amount)
    {
        //
    }
    The problem is when I try to declare a instance of the class I get the undefined errors.
    This is how I was declaring it.
    Code:
    X_Container<int> A(25);
    Thank you for your assistance.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The implementation of templates must be in header files.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Nice how tutorials forget to tell you things like that. So it is impossible to have functions using a template to be in a compiled library?
    is this a efficient way of resizing?
    Code:
    template<class Param>
    bool X_Container<Param>::Resize(int size)
    {
        delete []type;
        size_ = size;
        type = new Param[size];
        for ( int x = 0; x != size; ++x )
        {
            type[x] = NULL;
        }
        return true;
    }
    I know that vectors are better, but I would like to use my own containers.
    Last edited by Raigne; 06-17-2007 at 05:30 PM.

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    Quote Originally Posted by CornedBee View Post
    The implementation of templates must be in header files.
    You can separate template implementations if you have compiler that supports the export keyword, but those are few and far between.

    export also introduces a number of unforseen language issues, so even if you do have a capable compiler, it's probably best to keep your template implementations in the headers.
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

Popular pages Recent additions subscribe to a feed