Hi Guys, I am trying to teach myself templates and I keep getting lost in all these compilation errors. Here is the latest

// This is the interface for the base class
template<class V, class I =int>
class Array{

    private:
        vector<V> myStore;

    public:
        Array();
        Array(I size);
        Array(I size, V value);
        ~Array();

        int size();
        const V& element(I index);
        V& operator[] (I index);

        void print();
};

// This is the interface for the derived class
template<class V, class I=int>
class NumericArray: public Array<V,I>{

    public:
        NumericArray();
        NumericArray(I size){}
        NumericArray(int size, double value){}
        ~NumericArray(){}

//        void print();
};

This is my implementation for the constructor
template<class V, class I> NumericArray<V,I>::NumericArray(I size):Array<V,I>(size){

}
But this keeps giving me a compiler error. undefined reference to `Array<double, int>::Array(int)'. 

What am I doing wrong here? Any help would be much appreciated.

Where is your implementation of the constructor of Array, specifically the constructor that takes one argument? The compiler is telling you it cannot find it, neither can I from the code you showed. Your NumericArray constructor requires the definition of the Array constructor, which is missing, apparently.

I am slightly surprised that you have declared NumericArray as a template I would have expected something similar to

// This is the interface for the derived class
class NumericArray: public Array<double,int>{
...
}

That is the derived class is an implementation of the template for a given type, or even

typedef Array<double,int> NumericArray;

That asside remeber that a template is a pattern it is not actual code it is instructions to the compiler on how to create the code if required. This means that in general the (non-specialised) template must include implementations of all the methods it declares (accept pure virtual ones although I'm not entirely sure of the use case for a pure virtual funciton in a template).

None of the methods in your template Array have any implementations so you will almost always get this error when ever you try to call a method on the template.

When a template class is compiled for a specific type then the compiler creates all the methods that explicitly called or all methods if the template is explicitly instantiated. It compiles all these methods and attaches them to the object for the current file. It then does this with all the code files the template is used in so you get multiple compiled copies of the method. The reason that this doesn't cause a problem for the linker is that they are marked has having weak linkage, this marking means that the linker is able to through away all but one implementation of the method, you have no control of which one it keeps.

Anyway the main point is that all template methods should be implemented in the header that the template class is declared in, otherwise the compiler/linker will run into problems when instantiating compiling and linking the template.

@Mike
Here are the definitions for the functions

template<class V, class I> Array<V,I>::Array(){

}

template<class V, class I> Array<V,I>::Array(I size){
    this->myStore = vector<V>(size);
}

template<class V, class I> Array<V,I>::Array(I size, V value){
    this->myStore = vector<V>(size);
    for(int i =0;i<size;i++)
        this->myStore[i] =value ;    
}

template<class V, class I> Array<V,I>::~Array(){

}

They do. The issue was that my constructor were defined in the .cpp file which was not getting included in the child class. Apparently netbeans does not handle these things automatically (or I am doing some thing wrong)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.