0

Possible Duplicate:
Why can templates only be implemented in the header file?

classdeclaration.h

template<typename anytype>
class List;

template<typename anytype>
class Dict;

classdefinition.cpp

#include "classdeclaration.h"
#include<iostream>

using std::cout;
using std::endl;

template<typename anytype>
class List{
    private:
        int size;
        anytype * list;
    public:
        List(int a);
        List(const List<anytype> &a);
        int getSize();
        ~List();
};

template<typename anytype>
 List<anytype>::List(int a){
            size=a;
            list=new anytype[size];
            cout<<"List object initialized. The address of list is "<<list<<endl;
        }

template<typename anytype>
int List<anytype>::getSize(){
        return size;
        }

template<typename anytype>
List<anytype>::List(const List<anytype> &a){
        this.size=a.getSize();
        this.list=new List<anytype>(size);
        }

template<typename anytype>
List<anytype>::~List(){ 
            delete[] list;
            cout<<"List destructor called."<<endl;

}


template<typename anytype>
class Dict{
    private:
        int dicta;
        List<anytype> *dict;
    public:
        Dict(int num);
        Dict(const Dict<anytype>& a);
        int getDicta();
        ~Dict();
};

template<typename anytype> 
Dict<anytype>::Dict(int num){
            dicta=num;
            dict=new List<anytype>(dicta);
            cout<<"Dict object initialized. The address of dict is "<<dict<<endl;
        }

template<typename anytype>
int Dict<anytype>::getDicta(){
            return dicta;
        }

template<typename anytype>
Dict<anytype>::Dict(const Dict<anytype> & a){
            this.dicta=a.getDicta;
            dict=new Dict<anytype>(dicta);
        }

template<typename anytype> 
Dict<anytype>::~Dict(){
            delete[] dict;
            cout<<"Dict destructor called."<<endl;
        }

test.cpp

#include<iostream>
#include "classdeclaration.h"  //A


int main()
{
    using namespace std;
    Dict<int> a(10);
    cout<<"The address of Dict<int> a is "<<&a<<endl;
}

The question here is that the file will not compile with command "g++ test.cpp classdefinition.cpp -o e:\KC" and the compiler throws an error saying that Dict<int> a(10) has initializer but incomplete type. What does it mean by incomplete type and how can I fix it?

The other question is that if I replace the preprocessor statement in line A with #include "classdefinition.cpp", the file will compile but fail to run.

Cœur
  • 37,241
  • 25
  • 195
  • 267
JDein
  • 295
  • 2
  • 9

2 Answers2

3

You must place the definition of all templates in a header file. The compiler cannot find the definitions between translation units, unlike regular functions.

Also, how the hell am I supposed to know why your code will fail to run if you don't tell me what the error is?

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • When I run the program after the files were compiled, the program stops working and a window pops up, saying that the program "stopped working and was closed. a problem caused the application to stop working correctly.Windows will notify you if a solution is available". Why does the program stopped working? – JDein Mar 06 '12 at 05:47
  • @JDein: That's not "what the error is". That's "some nothing message from Windows which is identical between every error in every application". Use a debugger, and debug mode compilation, etc, to find out *what the error is*. – Puppy Mar 06 '12 at 12:20
  • bug?? Do you mean there are some logical mistakes in the code? – JDein Mar 07 '12 at 06:04
  • @JDein: No. It fails to execute because the Lords of Windows decided that it should, because they were drunk that day and threw a dart and it happened to land on your picture. – Puppy Mar 07 '12 at 12:12
  • = =|||,well, how can I avoid such problem when I write my code? – JDein Mar 07 '12 at 14:00
1

You should always place the full class definition in the header files, otherwise you will always get similar errors.

When it comes to templates, the full implementation also needs to be seen by the code using the class. This is because a templated class is not fully declared until you actually use it with template parameters.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for your advice, PileBorg. After I combined the "classdefinition.cpp" with "classdelcaration.h", the file will compile. But when I run the program, it stops working and pops up a window saying that the program "stopped working and was closed. a problem caused the application to stop working correctly.Windows will notify you if a solution is available". Are there any problems in my code? – JDein Mar 06 '12 at 05:39