Error No Acceptable Conversion

I am starting a new assignment on operator overloading. After performing a simple addition I get an error saying, " error C2679: binary '<<': no operator found which takes a right-hand operand of type 'cs_Fraction::Fraction' (or there is no acceptable conversion). Can someone please point out what the problem is?

The error is in the insertion operator with the cout statement in main.
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
 namespace cs_Fraction 
{
	class Fraction
	{
	private:
		int numerator;
		int denominator;

	public:
		Fraction(int inNumerator = 0, int indenominatoer = 1);
		friend Fraction operator + (const Fraction& left, const Fraction& right);
	};
}





#include <iostream>
#include "Fraction.h"
#include <cassert>
using namespace std;

namespace cs_Fraction {
Fraction operator + (const Fraction& left, const Fraction& right)
	{
		Fraction answer;
		answer.numerator = (left.numerator + right.numerator);
		answer.denominator = (left.denominator + right.denominator);

		return answer;
	}
}





#include <iostream>
#include "Fraction.h"
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
using namespace cs_Fraction;

int main()
{
	Fraction f1;
	Fraction f2(1,2);
	Fraction f3(1,2);
	
	f1 = f2 + f3;
	cout << f1;

	system("pause");
	return 0;
}
Last edited on
You get that error because you have not overloaded the << operator to work with your Fraction class.
Thank you for pointing that out. I tried creating a fix for that and now I am getting this error.
error C2248: 'Fraction::numerator': cannot access private member declared in class 'Fraction'

Updated code:
1
2
3
4
5
std::ostream& operator<<(std::ostream& out, const Fraction& right)
	{
			out << right.numerator << right.denominator;
			return out;
	}
You either have to declare it as a friend (like you did for operator+) or provide another way to access the numerator and denominator from outside the class.
I should have posted my updated class too.
1
2
3
4
5
6
7
8
9
10
11
12
class Fraction
	{
	private:
		int numerator;
		int denominator;

	public:
		Fraction(int inNumerator = 0, int indenominatoer = 1);
		friend std::ostream& operator<<(std::ostream& out, const Fraction& right);
		friend Fraction operator + (const Fraction& left, const Fraction& right);

	};
It seems to work fine.
You actually get it to work? Mine is still throwing that error.
Okay sorry, there is something going on with another with another function I started working on. I commented that out and now the cout << is working. Thank you.
Something is not making sense with a logic error though. I know the constructor initializes a fraction to 0/1 fine but when I send the argument (1,2) in a fraction object I cout it (no addition called) I get 12 and not not 1/2 like I'm expecting.
Last edited on
Well, the way you have the defined the << operator the numerator and denominator will be outputted right next to each other without anything in between.
Ok I see what you mean. Thank you, I appreciate it.
Topic archived. No new replies allowed.