I Have the following code in Student.h
#ifndef _student_h_
#define _student_h_
template <class T>
class Student
{
public:
Student();
~Student();
void setWage(float hourlyWage);
void addHours(float hoursWorked);
bool pay();
bool writeCheck(float value);
float getTotalEarnings();
float getStudentCut();
float getSwauCut();
float getWage();
private:
float wage;
float hours;
float swauCut;
float studentCut;
float totalEarnings;
};
#include "student.tpp" //Works with .tpp, errors with .cpp
#endif
As im trying to separate my code I attempted to place the following code into both a .cpp and a .tpp for testing.
#pragma once
#include "stdafx.h"
#include "Student.h"
template <class T>
Student<T>::Student()
{
wage = 0.0f;
hours = 0.0f;
swauCut = 0.0f;
studentCut = 0.0f;
totalEarnings = 0.0f;
}
template <class T>
Student<T>::~Student()
{
}
template <class T>
void Student<T>::setWage(float hourlyWage)
{
wage = hourlyWage;
}
template <class T>
void Student<T>::addHours(float hoursWorked)
{
hours += hoursWorked;
}
template <class T>
bool Student<T>::pay()
{
if (hours == 0 || wage == 0)
return false;
studentCut += .25*(hours * wage);
swauCut += .75*(hours * wage);
totalEarnings += hours * wage;
hours = 0.0f;
return true;
}
template <class T>
bool Student<T>::writeCheck(float value)
{
if (value < studentCut){
studentCut -= value;
return true;
}
return false;
}
template <class T>
float Student<T>::getTotalEarnings()
{
return totalEarnings;
}
template <class T>
float Student<T>::getStudentCut()
{
return studentCut;
}
template <class T>
float Student<T>::getSwauCut()
{
return swauCut;
}
template <class T>
float Student<T>::getWage()
{
return wage;
}
My issue is that if I use the .cpp file and comment out the tpp file I get all sorts of errors. However if I just #include Student.tpp the files compile fine and work. I was under the impression that cpp and tpp were relatively the same?
The errors im getting are:
Error1 error C2995: 'Student<T>::Student(void)' : function template has already been defined c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp 13 1 StudentProject
Error2 error C2995: 'Student<T>::~Student(void)' : function template has already been defined c:\users\aurelib.cs\desktop\studentproject\studentproject\student.cpp 18 1 StudentProject
.... for all the functions.
If I remove the #include "Student.h"
from the .cpp
file I get Syntax errors.
I am using Visual Studios. And like I said I have no problem when I #include "Student.tpp"
at the bottom of the template. But when I use the same code and #include "Student.cpp"
instead.
Thanks so much in advance!