Template Issues.

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 class template 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


Here are my HEADER files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
DOG.H

#ifndef DOG_H
#define DOG_H

#include <string>
using namespace std; 

template <typename T>
class Dog {
public:
	Dog (string, T); 	
	
private:
	string name; 
	T valueProperty;

};

#endif

EXPERIENCE.H

#ifndef EXPERIENCE_H
#define EXPERIENCE_H

#include <string>
using namespace std; 


class Experience {
public:
	 Experience(int); 	
	 int getExp();
	 bool operator>(const Experience &exp) const; //compare

private:
	int numYrsOfExperience; 

};

#endif


COST.H

#ifndef COST_H
#define COST_H

#include <string>
using namespace std; 


class Cost {
public:
	 Cost(double);
	 double getCost();
	 bool operator>(const Cost &cost) const; //compare
	
private:
	double price; 

};

#endif



CPP's

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
DOG.CPP

#include <iostream>
#include "Dog.h"
using namespace std; 

template< typename T>
Dog <T >::Dog(string name, T) {
	this->name=name;
	this->valueProperty=T;
}

EXPERIENCE.CPP

#include <iostream>
#include "Experience.h"
using namespace std; 

Experience::Experience(int numYrsOfExperience)
{this->numYrsOfExperience=numYrsOfExperience;}

int Experience::getExp(){
	return numYrsOfExperience;
}


bool Experience::operator >(const Experience &exp) const
{
	cout << this->numYrsOfExperience << " > " << exp. numYrsOfExperience<< endl;
	if (this-> numYrsOfExperience> exp.numYrsOfExperience)
	return true;
	else 
	return false;
}


COST.CPP

#include "Cost.h"
#include <iostream> 
using namespace std; 


Cost::Cost (double price) {
	this->price = price; 
}

double Cost::getCost(){
	return price; 
}

bool Cost::operator >(const Cost &cost) const
{
	cout << this->price << " > " << cost.price << endl;
	if (this->price > cost.price)
	return true;
	else 
	return false;
}


AND FINALLY DRIVER.CPP

#include "Dog.h"
#include "Cost.h"
#include "Experience.h"

int main() {

	Dog d = new Dog("test", Cost(20.00)); 
	
}


Thoughts? What is meant by the paramater list? Why does it say no acceptable default constructors?

Cheers,

RC325
Last edited on
I thought that templates need to be defined in the header file only?
I tried declaring it in the header only- either way I do it it doesn't change the outputted error.

Regards,

RC325
What is meant by the paramater list?

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; //  


That should get you started....
Topic archived. No new replies allowed.