operator overloading

I am trying to overload the << operator, i keep getting this error message:
/*
Box_it.cpp:39:68: error: ‘std::ostream& Box::operator<<(std::ostream&, const Box&)’ must take exactly one argument
std::ostream& operator<<(std::ostream& stream, const Box& showBox){
*/

I am failing to understand this error, because the code works without the operator<<() function. Thank you in advance.


#include <iostream>

class Box{
public:
Box(long long int length, long long int breadth, long long int width):length(length), width(width), breadth(breadth){
}
unsigned long long int getLength(){
unsigned long long int l;
std::cin >> l;
return l;
}
unsigned long long int getBreadth(){
unsigned long long int b;
std::cin >> b;
return b;
}
unsigned long long int getWidth(){
unsigned long long int w;
std::cin >> w;
return w;
}

bool operator<(const Box &otherBox) const{
bool x=true,y=false;

if(length<otherBox.length){
return x;
}
else if(breadth<otherBox.breadth && length==otherBox.length){
return x;
}
else if(width<otherBox.width && breadth==otherBox.breadth && length==otherBox.length){
return x;
}
else{
return y;
}
}
std::ostream& operator<<(std::ostream& stream, const Box& showBox){

stream << showBox.length << " " << showBox.breadth << " " << showBox.width;
return stream;
}

private:
unsigned long long int length;
unsigned long long int width;
unsigned long long int breadth;
};



int main(){
Box A(0,0,0), B(0,0,0);
A.getLength();
A.getBreadth();
A.getWidth();

B.getLength();
B.getBreadth();
B.getWidth();

return 0;
}
Last edited on
You need to define operator<< outside the class (just after it).
Or you can define the overloads as friend functions:

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
#include <iostream>

class Box
{
private:
	long length, breadth, width;
public:
	Box(long l, long b, long w): length(l), breadth(b), width(w) {}
	friend bool operator<(const Box& lhs, const Box& otherBox)
	{
		long volume = lhs.length * lhs.breadth * lhs.width;
		long other = otherBox.length * otherBox.breadth * otherBox.width;
		if(volume < other) return true;
		else return false;
	}
	friend bool operator>(const Box& lhs, const Box& otherbox)
	{
		return (!(lhs<otherbox)); // > is just the opposite of <
	}

	friend std::ostream& operator<<(std::ostream& stream, const Box& box)
	{
		stream << "L: " << box.length << "\tB: " << box.breadth << "\tW: " << box.width;
		return stream;
	}

	friend std::istream& operator>>(std::istream& stream, Box& box) // We can do the same with istream just like ostream
	{
		stream >> box.length >> box.breadth >> box.width;
		return stream;
	}
	long GetVolume() { return length * breadth * width; }
};

int main(){
	Box A(20,30,40), B(15,45,20);

	std::cout << A << "\n";
	std::cout << B << "\n";

	std::cout << "A volume: " << A.GetVolume() << "\n";
	std::cout << "B volume: " << B.GetVolume() << "\n";

	std::cout << "Is A greather than B? " << (A>B) << "\n";

	std::cout << "Set 3 values for A: ";
	std::cin >> A;

	std::cout << A << "\n";
	std::cout << B << "\n";

	std::cout << "A volume: " << A.GetVolume() << "\n";
	std::cout << "B volume: " << B.GetVolume() << "\n";

	std::cout << "Is A greather than B? " << (A>B) << "\n";
	return 0;
} 


Edit: Fixed some extra whitspaces in places
Last edited on
Topic archived. No new replies allowed.