I am trying to understand how to pass a class as a Type parameter to another class.
I can't seem to figure out what the issue is ..
I get the error
1 2 3 4 5
c:\documents and settings\ipb\my documents\visual studio 2008\projects\templatedog\templatedog\driver.cpp(7) : error C2955: 'Dog' : use of classtemplate requires template argument list
c:\documents and settings\ipb\my documents\visual studio 2008\projects\templatedog\templatedog\dog.h(8) : see declaration of 'Dog'
c:\documents and settings\ipb\my documents\visual studio 2008\projects\templatedog\templatedog\driver.cpp(7) : error C2514: 'Dog' : class has no constructors
c:\documents and settings\ipb\my documents\visual studio 2008\projects\templatedog\templatedog\dog.h(8) : see declaration of 'Dog'
c:\documents and settings\ipb\my documents\visual studio 2008\projects\templatedog\templatedog\driver.cpp(7) : error C2512: 'Dog' : no appropriate default constructor available
A variable of a template class has to be declared lke this:
Dog<Cost> d;
Creating a variable without specifying construction paratmeters invokes the default constructor.
If you specify a contructor with parameters (which you have done), then you are also
responsible for the default constructor not the compiler.
So you need to provide default constructors for Dog, Cost and Experience (or possibly make use
of initialization lists)
Note that new operator returns a pointer:
So Dog<Cost> d = new Dog("test", Cost(20.00)); is an error
It should be Dog<Cost>* d = new Dog("test", Cost(20.00));//using new
or Dog<Cost> d("test", Cost(20.00));
This is also an major misunderstanding of how templates work
1 2 3
Dog <T >::Dog(string name, T) {
this->name=name;
this->valueProperty=T; // This is an error
Should be something like:
1 2 3
Dog <T >::Dog(string name, T t) {
this->name=name;
this->valueProperty = t; //