operator overload outside of classes

I have a question about why I am getting an error when trying to overload an operator outside of a class. I do not understand what is happening. I get this error:
Error 1 error C2665: 'std::operator !=' : none of the 5 overloads could convert all the argument types Line 62


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
/*Matthew Brooks
1/23/2015
Throttle class overload
*/

#include<iostream>
using namespace std;

class throttle
{
public:
	throttle(int tPos = 6, int cPos = 0)
	{
		Top_Position = tPos;
		position = cPos;
	}
	throttle copy(throttle t)
	{
		Top_Position = t.getTop();
		position = t.getPos();
	}

	int getTop() const
	{
		return Top_Position;
	}

	int getPos() const
	{
		return position;
	}

	bool operator==(throttle t)
	{
		return ((Top_Position == t.getTop()) && (position == t.getPos()));
	}

private:
	int position;
	int Top_Position;


};

int main()
{
	throttle car;
	throttle truck(30);
	throttle shuttle(20, 6);
	throttle mythrottle(truck);

	if (car.operator==(truck))
		cout << "yes" << endl;
	else
		cout << "no" << endl;

	if (car == truck)
		cout << "yes" << endl;
	else
		cout << "no" << endl;

	if (operator!=(car, truck))    //this is where I get the error when
		cout << "yes" << endl; //tyring to use the operator!= function
	else
		cout << "no" << endl;

	/*if (car != truck)
		cout << "yes" << endl; commented out to get the first way to 
	else                           work 
		cout << "no" << endl;
	*/	
	return 0;
}

bool operator!=(const throttle& t1, const throttle& t2)
{
	return ((t1.getTop() != t2.getTop()) && (t1.getPos() != t2.getTop()));
	
}
you only need to call your operators in the normal way, like this:
if (car == truck)
Last edited on
I know but this is a homework assignment so I have to do it both ways but I can't figure out what I am doing wrong using the function outside of the class.
I read that and understand how to write an overload function like that using a member function inside a class, but I need to be able to write a regular function outside of a class that can do the same thing.
Your code as compiler sees it at the moment of the error.
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
#include<iostream>
using namespace std;
class throttle
{
public:
	throttle(int tPos = 6, int cPos = 0)
	{
		Top_Position = tPos;
		position = cPos;
	}
	throttle copy(throttle t)
	{
		Top_Position = t.getTop();
		position = t.getPos();
	}
	int getTop() const
	{
		return Top_Position;
	}
	int getPos() const
	{
		return position;
	}
	bool operator==(throttle t)
	{
		return ((Top_Position == t.getTop()) && (position == t.getPos()));
	}
private:
	int position;
	int Top_Position;
};
int main()
{
	throttle car;
	throttle truck(30);
	throttle shuttle(20, 6);
	throttle mythrottle(truck);

	if (car.operator==(truck))
		cout << "yes" << endl;
	else
		cout << "no" << endl;

	if (car == truck)
		cout << "yes" << endl;
	else
		cout << "no" << endl;

	if (operator!=(car, truck))    //Where is operator!= declared? 
I declared it in line 75 after my main program
The compiler needs to know what function you're talking about before you actually call it. Because you've declared the function AFTER you attempt to use it on line 62 of your original code, you're compiler is throwing an error because you've broken a syntax rule. You need to make a function prototype before your main function, and you'll be good to go.
Thank you so much! I can't believe it was such a simple error lol I really appreciate all the feedback on this thread.
Topic archived. No new replies allowed.