undefind refrence to when using templates

When I try and compile my my files the compiler keeps telling me i have an undfiend refrence to a my templates when i give them a type i dont know why it would do this cause they are defined on in second .cc file

This is the error that it gives me:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Employee_driver.o: In function `main':
/home/tyler/Desktop/P2/Employee_driver.cc:23: undefined reference to `Manager<int>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:24: undefined reference to `Manager<char>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:25: undefined reference to `Manager<bool>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:27: undefined reference to `HourlyWorker<int>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:28: undefined reference to `HourlyWorker<char>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:29: undefined reference to `HourlyWorker<bool>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:31: undefined reference to `OnCommission<int>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:32: undefined reference to `OnCommission<char>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:33: undefined reference to `OnCommission<bool>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:59: undefined reference to `Manager<int>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:63: undefined reference to `Manager<char>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:67: undefined reference to `Manager<bool>::Manager(double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:78: undefined reference to `HourlyWorker<int>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:82: undefined reference to `HourlyWorker<char>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:86: undefined reference to `HourlyWorker<bool>::HourlyWorker(double, double, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:98: undefined reference to `OnCommission<int>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:102: undefined reference to `OnCommission<char>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/tyler/Desktop/P2/Employee_driver.cc:106: undefined reference to `OnCommission<bool>::OnCommission(double, int, int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
collect2: ld returned 1 exit status
make: *** [Employee] Error 1 


Employee.h
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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <iomanip>
#include <unistd.h>
#include <stdlib.h>
using namespace std;

const int size = 10;

template <class T>
class Employee
{
	protected: 
		int ID;
		string fullName;
		T classification;
	public: 
		Employee(int = 0, string = "");
		double earnings();
		void print() const;
};

template <class T>
class Manager: public Employee <T>
{
	private:
		double salary;
	public:
		Manager(double = 0.0, int = 0, string = ""); 
		double getSalary();
		void print();
};


template <class T>
class HourlyWorker: public Employee <T>
{
	private:
		double hoursWorked;
		double payRate;
	public:
		HourlyWorker(double = 0.0, double = 0.0, int = 0, string = "");
		double getPay();
		void print();
};	


template <class T>
class OnCommission: public Employee <T>
{
	private:
		double itemRate;
		int itemsMade;
	public:
		OnCommission(double = 0.0, int = 0, int = 0, string = "");
		double getPay();
		void print();
};


Employee.cc
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
#include "Employee.h"

template <class T>
void Employee<T>::print() const
{
	cout << fullName << endl;
}

template <class T>
void HourlyWorker<T>::print() 
{
	Employee<T>::print();
	cout << " earns " << getPay() << endl;
}

template <class T>
double HourlyWorker<T>::getPay() 
{
	return hoursWorked*payRate;
}

template <class T>
void Manager<T>::print()
{
	Employee<T>::print();
	cout << " earns " << getSalary() << endl;
}

template <class T>
double Manager<T>::getSalary() 
{
	return salary;
}

template <class T>
void OnCommission<T>::print()  
{
	Employee<T>::print();
	cout << " has wages " << getPay() << endl;
}

template <class T>
double OnCommission<T>::getPay() 
{
	return itemRate*itemsMade;
}


Employee_driver.cc
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "Employee.h"

int main()
{
	int pos = 0;
	int temp = 0;
	int tempPos = 0;
	int answer = 0;
	string name;
	int level = 0;
	char Class;
	bool tp;
	int classification[10];
	int itemsMade = 0;
	double managerSalary = 0;
	double hourlyRate = 0;
	double hoursWorked = 0;
	double itemRate = 0;
	bool ex = false;
	
	
	
	Manager<int> manArrayLevel[size]; //manager of different levels 1, 2, 3
	Manager<char> manArrayClass[size]; 
	Manager<bool> manArrayTempPerm[size];
	
	HourlyWorker<int> workerArrayLevel[size]; 
	HourlyWorker<char> workerArrayClass[size]; 
	HourlyWorker<bool> workerArrayTempPerm[size];

	OnCommission<int> commissionArrayLevel[size]; 
	OnCommission<char> commissionArrayClass[size]; 
	OnCommission<bool> commissionArrayTempPerm[size];
	
	while (ex != true)
	{
		cout << "1. Add new Employee" << endl;
		cout << "2. List all Employees" << endl;
		cout << "3. Exit" << endl;
		cin >> answer;		
		
		if (answer == 1)
		{
			cout << "What type of Employee are you entering?:" << endl;
			cout << "1. Manager" << endl;
			cout << "2. Hourly Worker" << endl;
			cout << "3. Commission Worker" << endl;
			cin >> answer;

			if (answer == 1) //Manager
			{
				cout << "What is the full name of this Manager?" << endl;
				cin >> name;
				
				cout << "What level are they?" << endl;
				cin >> level;
				manArrayLevel[pos] = level;
				
				cout << "What class are they?" << endl;
				cin >> Class;
				manArrayClass[pos] = Class;
				
				cout << "Are they temporary or permeniant?" << endl;
				cin >> tp;
				manArrayTempPerm[pos] = tp;
				
				
			}
			else if (answer == 2) //HourlyWorker
			{
				cout << "What is the full name of this Worker?" << endl;
				cin >> name;
				
				cout << "What level are they?" << endl;
				cin >> level;
				workerArrayLevel[pos] = level;
				
				cout << "What class are they?" << endl;
				cin >> Class;
				workerArrayClass[pos] = Class;
				
				cout << "Are they temporary or permeniant?" << endl;
				cin >> tp;
				workerArrayTempPerm[pos] = tp;
				
				//add hours and pay rate 
			
			}
			else if (answer == 3) //CommissionWorker
			{
				cout << "What is the full name of this Commission Worker?" << endl;
				cin >> name;
				
				cout << "What level are they?" << endl;
				cin >> level;
				commissionArrayLevel[pos] = level;
				
				cout << "What class are they?" << endl;
				cin >> Class;
				commissionArrayClass[pos] = Class;
				
				cout << "Are they temporary or permeniant?" << endl;
				cin >> tp;
				commissionArrayTempPerm[pos] = tp;
				
				//add item rate and items made
			}
		}//end big if 
	}//end while
}
You might want to consider all your template definitions into your header file. This is a little "quirk" if you will of templates: you see, they don't compile into binary with the classes, functions, etc, so while there are no symbol redefinition errors, at the same time, if you implement them in a source file for multiple files to use it's a moot point. :)

-Albatross
i have tried putting the template definitions in the Employee_driver.cc files but i still get the same error from the compiler and now I am thinking it could maybe be because by constructor parameters for each different class take values but i am not giving any when i try to make the arrays, but what i am trying to do is make arrays of the different types of classes so that i can add different things to the arrays passing the correct information to the constructor at that time
Put all the codes of Employee.cc in the Employee.h and see what happens.
Every part of the template definition should be in the header, you should not put the implementation into a .cc file like you would with a normal class/function.
@alwaysLearning0
i tried combining the Employee.cc file with the Employee.h file into one .h file and i still get the same error. And after reading that link i tried just putting the template class Employee<int> and so forth at the end of the Employee.cc file and still got the same error. I even tired putting everything into one .cc file and i still got the error.

any other ideas?
In addition to putting the definitions in the header file you also need to write definitions for these constructors:
1
2
3
4
Employee(int = 0, string = "");
Manager(double = 0.0, int = 0, string = "");
HourlyWorker(double = 0.0, double = 0.0, int = 0, string = "");
OnCommission(double = 0.0, int = 0, int = 0, string = "");

They appear to be missing.



wow cant believe that i forgot to define those thanks Galik
Topic archived. No new replies allowed.