C++ - Error in Overloading ostream
Hello,
I have an error when I'd to return output. (in Visual Studio 2019)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#pragma once
#include<iostream>
class rational
{
rational();
rational(long, long);
rational operator+(rational);
rational operator-(rational);
rational operator*(rational);
rational operator/(rational);
bool operator<(rational);
bool operator<=(rational);
bool operator>(rational);
bool operator>=(rational);
bool operator==(rational);
bool operator!=(rational);
friend std::ostream operator<<(std::ostream, rational);
friend std::istream operator>>(std::istream, rational);
private:
int denom, numer;
};
|
1 2 3 4 5 6 7 8 9
|
#include "rational.h"
std::ostream operator<<(std::ostream output, rational rhs)
{
output << "result: " << rhs.numer << "/"
<< rhs.denom;
return output;
}
|
1 2
|
Severity Code Description Project File Line Suppression State
Error C2248 'std::basic_ostream<char,std::char_traits<char>>::basic_ostream': cannot access protected member declared in class 'std::basic_ostream<char,std::char_traits<char>>' C++_6 C:\Users\My\source\repos\C++_6\C++_6\rational.cpp 8
|
1 2
|
Severity Code Description Project File Line Suppression State
Error (active) E0330 "std::basic_ostream<_Elem, _Traits>::basic_ostream(std::basic_ostream<_Elem, _Traits> &&_Right) [with _Elem=char, _Traits=std::char_traits<char>]" (declared at line 43 of "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29910\include\ostream") is inaccessible C++_6 C:\Users\My\source\repos\C++_6\C++_6\rational.cpp 8
|
Your class defines every part as being private. All of your methods and members, including the friendly operator I/O operators.
If you don't specify an access type it defaults to private in a class, the opposite of a struct.
Thank you, I wrote public: for all methods but the problem doesn't solve.
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
|
#include <iostream>
class rational
{
public:
rational();
rational(long, long);
rational operator+(rational);
rational operator-(rational);
rational operator*(rational);
rational operator/(rational);
bool operator<(rational);
bool operator<=(rational);
bool operator>(rational);
bool operator>=(rational);
bool operator==(rational);
bool operator!=(rational);
friend std::ostream &operator<<(std::ostream&, const rational&);
friend std::istream &operator>>(std::istream&, rational&);
private:
int denom, numer;
};
std::ostream& operator<<(std::ostream& output, const rational& rhs)
{
output << "result: " << rhs.numer << "/"
<< rhs.denom;
return output;
}
std::istream& operator>>(std::istream& input, rational& rhs)
{
input >> rhs.numer >> rhs.denom;
return input;
}
|
Topic archived. No new replies allowed.