i have a 'triple' template, which takes three template items. it has three template functions, min, med and max, which return the lowest, median and maximum value of what's passed to the triple template. i've made an employee class with member variables name, salary.
i've to create three employee objects, put them in a triple template and call on the functions of the template class to find the employee with the lowest and highest salary. i've overloaded the < and > operator to achieve this but am getting this error
error C2678: binary '>' : no operator found which takes a left-hand operand of type 'const employee' (or there is no acceptable conversion)
twice which is in the max fucntion of the triple template class, which is below (bold lines indicate errors):
1 2 3 4 5 6 7 8 9 10 11
template<typename T>
const T& triple<T>::max() const
{
T max = first;
if(second > max)
max = second;
if(third > max)
max = third;
return max;
}
employee emp1("Mark", 25000);
employee emp2("Peter", 105000);
employee emp3("Brian", 75000);
triple<employee>empTemplate(emp1, emp2, emp3);
employee highestSalary = empTemplate.max();
cout<<"Employee with the highest salary is "<<highestSalary.getName()<<" with a salary of €"<<highestSalary.getSalary()<<endl;
tried that, declared the two overloaded const verions with const at the end, then defined them again in employee.cpp, but now i'm getting more errors?? am i doing it right? why copy and paste, ie define them again when i could just add const to the one's that are already there?
the original form that the operator took (in the first post) was given to us in our labsheet, so i'm suprised that there's this error...or maybe it was supposed to catch us out? unlikely...
here's the errors:
Error 7 error LNK1169: one or more multiply defined symbols found F:\S6\SDev 6\Labsheet 3\Lab 3\Debug\Lab 3.exe 1 1 Lab 3
as a matter of interest do you use visual studio 2010 or another verion? sometimes it throws errors, as in red underlined code, that make no sense, and then they just clear themselves, but before they i might make a change to code and it can throw me off completely.
Are your template functions defined in the template header or in a separate cpp? By the looks of it, you have defined them in their own cpp, which is fine if only that compilation unit (that class) uses the template, but in your case it's not because you use it in main. Maybe you can list out your files?
okay did that, it compiles but i'm getting this exception:
std::bad_alloc at memory location (some memory address). i havn't come across this error before...
the only reason i included the .cpp files in main was because i was getting some linking errors and this seemed to take care of the problem. i wouldn't normally do it.
do you have any idea what could be causing the exception?