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.