overloaded operator error/problem

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;

}


here's the overloaded operators

1
2
3
4
5
6
7
8
9
bool employee::operator<(const employee &emp)
{
	return(salary < emp.salary);
}

bool employee::operator>(const employee &emp)
{
	return(salary > emp.salary);
}


and here's what i'm doing in main

1
2
3
4
5
6
7
8
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;


anyone any ideas?
These can only be used on non-const objects of type employee:
1
2
3
4
5
6
7
8
9
bool employee::operator<(const employee &emp)
{
	return(salary < emp.salary);
}

bool employee::operator>(const employee &emp)
{
	return(salary > emp.salary);
}


You have not defined the overloaded const versions. Just copy paste and put const at the end.
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?
You could do just that and have only the const ones, no problem with that. What are your other errors now?


but now i'm getting more errors?? am i doing it right? Yes, if you are comparing const objects you have to have a const operator.
here's how i changed it:

1
2
bool operator < (const employee &emp) const;
bool operator > (const employee &emp) const;


1
2
3
4
5
6
7
8
9
bool employee::operator<(const employee &emp) const
{
	return(salary < emp.salary);
}

bool employee::operator>(const employee &emp) const
{
	return(salary > emp.salary);
}


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

Error 2 error LNK2005: "public: __thiscall employee::employee(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,double)" (??0employee@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@N@Z) already defined in employee.obj F:\S6\SDev 6\Labsheet 3\Lab 3\Lab 3\main.obj Lab 3

Error 3 error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall employee::getName(void)const " (?getName@employee@@QBE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) already defined in employee.obj F:\S6\SDev 6\Labsheet 3\Lab 3\Lab 3\main.obj Lab 3

Error 4 error LNK2005: "public: double __thiscall employee::getSalary(void)const " (?getSalary@employee@@QBENXZ) already defined in employee.obj F:\S6\SDev 6\Labsheet 3\Lab 3\Lab 3\main.obj Lab 3


Error 5 error LNK2005: "public: bool __thiscall employee::operator<(class employee const &)const " (??Memployee@@QBE_NABV0@@Z) already defined in employee.obj F:\S6\SDev 6\Labsheet 3\Lab 3\Lab 3\main.obj Lab 3


Error 6 error LNK2005: "public: bool __thiscall employee::operator>(class employee const &)const " (??Oemployee@@QBE_NABV0@@Z) already defined in employee.obj F:\S6\SDev 6\Labsheet 3\Lab 3\Lab 3\main.obj 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?
triple.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TRIPLE_H_
#define TRIPLE_H_

template<typename T>
class triple
{
public:
	triple(const T& t1, const T& t2, const T& t3);
	const T& min() const;
	const T& med() const;
	const T& max() const;
private:
	T first, second, third;
};

#endif 


employee.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef EMPLOYEE_H_
#define EMPLOYEE_H_
#include<string>
using namespace std;

class employee
{
public:
	employee(){}
	employee(string n, double s);
	string getName() const;
	double getSalary() const;
	bool operator < (const employee &emp) const;
	bool operator > (const employee &emp) const;
private:
	string name;
	double salary;
};

#endif 


triple.cpp
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
#include"triple.h"

template<typename T>
triple<T>::triple(const T& t1, const T& t2, const T& t3)
{
	first=t1;
	second=t2;
	third=t3;
}

template<typename T>
const T& triple<T>::min() const
{
	T min = first;
	if(second < min)
		min = second;
	if(third < min)
		min = third;
	return min;
}

template<typename T>
const T& triple<T>::med() const
{
	T med = first;
	if(first > second && first < third)
		return first;
	else if(second > first && second < third)
		return second;
	else return third;
}

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.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include"employee.h"

employee::employee(string n, double s)
{
	name = n;
	salary = s;
}

string employee::getName() const{return name;}
double employee::getSalary() const{return salary;}

bool employee::operator<(const employee &emp) const
{
	return(salary < emp.salary);
}

bool employee::operator>(const employee &emp) const
{
	return(salary > emp.salary);
}

	 


main.cpp - commented out code is from an earlier section of the labsheet
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
#include"arrayT.h"
#include"arrayT.cpp"
#include"triple.h"
#include"triple.cpp"
#include"employee.h"
#include"employee.cpp"
#include<iostream>

using namespace std;

int main()
{
	/*arrayT<int, 10> theArray;

	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);
	theArray.addEntry(5);

	theArray.totalValue();

	triple<int> intTemplate(55,100,78);
	cout<<"Min int value: "<<intTemplate.min()<<endl;
	cout<<"Med int value: "<<intTemplate.med()<<endl;
	cout<<"Max int value: "<<intTemplate.max()<<endl;

	triple<char>charTemplate('z', 'f', 'g');
	cout<<"Min char value: "<<charTemplate.min()<<endl;
	cout<<"Med char value: "<<charTemplate.med()<<endl;
	cout<<"Max char value: "<<charTemplate.max()<<endl;*/

	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;

	system("PAUSE");
	return 0;
} 
Move everything in your Tripple.cpp into the Tripple header, you can just paste it at the bottom (after the class Declaration).

Also, don't include your .cpp's in main, only your headers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TRIPLE_H_
#define TRIPLE_H_

template<typename T>
class triple
{
public:
	triple(const T& t1, const T& t2, const T& t3);
	const T& min() const;
	const T& med() const;
	const T& max() const;
private:
	T first, second, third;
};

//---------------------------
//Paste your stuff here...
//----------------------------
#endif  
Last edited on
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?
std::bad_alloc is thrown when new fails to allocate memory.
why would new fail to allocate memory? is this an error on my behalf?
Topic archived. No new replies allowed.