Problem with << operator

I'm working on problem 6 of Euler but the issue I'm having isn't necessarily related to the answer to the problem. I'm attempting to solve the problem using OOP because I really need to get better at it and I keep receiving this error after trying to compile:

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Euler6' (or there is no acceptable conversion)

Here is my code that I have.

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

using namespace std;

class Euler6{
	float x;
public:
	Euler6 ();
	float sumofsquares (float);
	float squareofsum (float);
	Euler6 operator - (Euler6);
};


Euler6 Euler6::operator- (Euler6 param){
	Euler6 temp;
	
	temp.x = x - param.x;

	return temp;
}


float Euler6::sumofsquares (float x){
	
	float sum = 0;

	while (x <= 100)
	{
		sum += pow(x,2);
	}

	return sum;
}

float Euler6::squareofsum (float x){

	float sum = 0;

	while (x <= 100)
	{
		sum += x;
	}

	sum = pow(sum, 2);

	return sum;
}

Euler6::Euler6 (){
	x = 1;
}

int main (){
	Euler6 x;
	Euler6 y;
	Euler6 z;
	
	x.sumofsquares (1.0);
	y.squareofsum (1.0);

	z = y - x;


	cout << "The difference is: " << z;

	return 0;
}


Any help as to what I can do to fix this error would be greatly appreciated.
You need to overload operator<< to work with your Euler6 class.

1
2
// global overload, not inside Euler6 (prototype)
ostream & operator << (ostream &, const Euler6 &);


http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.8

Edit: there are other approaches... like:
- making your float x; public and simply cout << z.x;
- writing a conversion operator like operator float() const {return x;}
Last edited on
Thanks a lot it works fine now. I did this:

1
2
3
4
5
6
friend std::ostream& operator << (std::ostream& out, Euler6 const& euler);

std::ostream& operator << (std::ostream& out, Euler6 const& euler)
{
	return out << euler.x;
}


I also fixed the infinite loops I had above and they are producing the correct results but how do I get them to send the correct numbers back to the main program. When I run the program and debug it, it shows that the correct values are going into sum but when I return sum the value of x and y becomes 1 and it keeps saying my difference is zero.
You never assign the sum value to anything persistent. You calculate the sum value in a local value in each function, and then return the sum. The returned value is ignored (you never do anything with it.) You never re-assigned the x value in either "x" or "y" (by the way, consider renaming something so you don't have "x" as an Euler6 object and "x" and as a data member).

So, when you return from the 2 function calls, "x" and "y" are unchanged.

You must either modify ".x" in your functions or capture the return values from the function calls.
Perfect. Thank you so much for your explanation. Everything works well now. I just changed the functions to void and assigned sum to x.

Thank you for your help..both of you.
Topic archived. No new replies allowed.