using union and I need a ">" overloaded operator. How would I achieve this?

How would I achieve this. I keep getting errors. I tried to return bool values and then switched the return type on the overloaded function to bool but it still doesn't work.
Let's see your code, then!?

Andy
it's just a test case but I ahve also a more advanced program with code like this but m ore complex. Is there a way to actually test to see which variables contain values or which ones you want to use(i know setting one variable in a union will define all since occupying the same memory)... Possibly could I make a union and an int value in a union just go to an int argument? idk...

Could someone also give me a summary on how to overload operators with different types of the actual class being the return type.
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
#include <iostream>

union Bilo {
	int i, g;
	double c;
	struct  {
		short hi;
		short blue;
	} flu;
	Bool operator> (Bilo);
};
void check (Bilo blue, Bilo red);

int main()
{
	Bilo FluShot;
	FluShot.flu.hi = 32;
	FluShot.flu.blue = 2200;
	
	Bilo CluShot;
	CluShot.i = 32;
	check(FluShot, CluShot);

	
	return 0;
}


void check(Bilo blue, Bilo red) {
	if (blue > red)
		std::cout << "Blue is greater than Red" << std::endl;
	else
		std::cout << "Red is greater than Blue" << std::endl;
}
	

bool Bilo::operator> ( Bilo param) {
	if (flu.hi > param.i)
		return true;
	else
		return false;
};
Last edited on
The only syntactical problem with that code is bool should be lower case here:

Bool operator> (Bilo);

Another thing is comparing flu.hi with param.i imposes an additional meaning to the placement of the operands in the inequality. Consider only comparing the same members in the operator >, or use a simple function instead of the operator for clarity.
Last edited on
werddd. Bizarre mistake.
Topic archived. No new replies allowed.