How to rectify private error while using Friend Function in C++? - c++

Getting this error while using friend function in C++ : error: ‘int complex::a’ is private within this context. How will I rectify this error? I have created one complex class and while learning a friend function, i get to know that friend function can access private member functions too. But in this code, this error pops out. Thanks in advance.
#include <iostream>
using namespace std;
class complex{
private:
int a, b;
public:
void setNumber(int x,int y){a=x;b=y;}
void getNumber(){cout << "\n a="<< a << "b=" << b; }
friend ostream& operator <<(ostream&, complex);
friend istream& operator >>(istream&, complex&);
};
ostream& operator <<(ostream &dout, complex c){
cout << "a=" << c.a;
cout << "b=" << c.b;
return (dout);
}
istream& operator <<(istream &din, complex &c){
cin>>c.a>>c.b;
return (din);
}
int main(){
complex c1;
cin >> c1;
cout << c1;
return 0;
}

change this
ostream& operator <<(ostream &dout, complex c)
{
cout << "a=" << c.a;
cout << "b=" << c.b;
return (dout);
}
istream& operator <<(istream &din, complex &c)
{
cin>>c.a>>c.b;
return (din);
}
to
ostream& operator <<(ostream &dout, const complex& c)
{
dout << "a=" << c.a;
dout << "b=" << c.b;
return (dout);
}
istream& operator >>(istream &din, complex &c)
{
din>>c.a>>c.b;
return (din);
}

Related

Input/Output Operators Overloading in C++

Stuck with ostream/istream operator overloading
Q1. why we are using ostream& operator as friend?
Q2. Why we are passing two arguments in ostream & operator << (ostream &out, const Complex &c)
Q3. why we are referencing to Cout and in ? istream & operator >> (istream &in, Complex &c).
Ref to: Overloading stream Oerator overloading- Geeks4Geeks
HERE IS THE CODE
#include <iostream>
using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c)
{
out << c.real;
out << "+i" << c.imag << endl;
return out;
}
istream & operator >> (istream &in, Complex &c)
{
cout << "Enter Real Part ";
in >> c.real;
cout << "Enter Imaginary Part ";
in >> c.imag;
return in;
}
int main()
{
Complex c1;
cin >> c1;
cout << "The complex object is ";
cout << c1;
return 0;
}
A1. To access the private members
A2. The first argument is the stream and the second is the object. operator<< and operator>> expect two arguments
A3. Because they are modified in the function. The functions read from resp. write into the stream.
In addition:
Don't use using namespace std;
Don't initialize members in the constructor body. Use the constructor initializer list
Complex(int r = 0, int i =0)
{ real = r; imag = i; }
should be
Complex(int r = 0, int i = 0) : real(r), imag(i) {}

Problems with ostream

I'm working in a Big Integer implementation in C++ and I'm trying to use cout with my BigInt class. I already overloaded the << operator but it doesn't work in some cases.
Here is my code:
inline std::ostream& operator << (ostream &stream, BigInt &B){
if (!B.getSign()){
stream << '-';
}
stream << B.getNumber();
return stream;
}
The code above works with:
c = a + b;
cout << c << endl;
But fails with:
cout << a + b << endl;
In the first case the program runs fine, but in the second the compiler gave an error:
main.cc: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
It's possible to overload the << operator for function in both cases?
Methods:
string getNumber ();
bool getSign ();
string BigInt::getNumber (){
return this->number;
}
bool BigInt::getSign (){
return this->sign;
}
As chris already pointed out in comments very quickly (as usual), you have a temporary created in here:
cout << a + b << endl;
You cannot bind that to a non-const reference. You will need to change the signature of your operator overloading by adding the const keyword to the reference.
This code works for me with a dummy BigInt implementation (as you have not shared yours):
#include <iostream>
using namespace std;
class BigInt
{
public:
bool getSign() const { return true; }
int getNumber() const { return 0; }
const BigInt operator+(const BigInt &other) const {}
};
inline std::ostream& operator << (ostream &stream, const BigInt &B){
// ^^^^^
if (!B.getSign()){
stream << '-';
}
stream << B.getNumber();
return stream;
}
int main()
{
BigInt a, b, c;
c = a + b;
cout << c << endl;
cout << a + b << endl;
return 0;
}
But yeah, I agree that the error message is not self-explanatory in this particular case.
Change
inline std::ostream& operator << (ostream &stream, BigInt &B){
to
inline std::ostream& operator << (ostream &stream, BigInt const& B){
c can be a used where BiInt& is expected but a+b cannot be because a+b is a temporary. But it can be used where BigInt const& is expected.

Overloading casting Operator: C++

I am trying to cast two classes Pounds and Cents.
#include <iostream>
using namespace std;
class Pounds;
//Overloading Casting Operators: Pounds and Cents
class Cents{
int mCents;
public:
Cents(int n);
friend ostream& operator<< (ostream& os,const Cents& c);
operator Pounds();
};
class Pounds{
int mPounds;
public:
Pounds(int n);
operator Cents();
friend ostream& operator<< (ostream& os,const Pounds& p);
};
Cents::Cents(int n):mCents(n){}
ostream& operator<< (ostream& os,const Cents& c){
os << c.mCents;
return os;
}
Cents::operator Pounds(){
double d=mCents/100.0 ;
cout << d << endl;
return d;
}
Pounds::Pounds(int n):mPounds(n){}
Pounds::operator Cents(){
return mPounds*100;
}
ostream& operator<< (ostream& os,const Pounds& p){
os << p.mPounds;
return os;
}
int main()
{
Pounds Pnd(4);
cout << (Cents) Pnd << endl;
Cents Cnt(456);
cout.precision(3);
cout << showpoint << (Pounds) Cnt << endl;
return 0;
}
My question is very simple. Why the output is showing as:
$ ./Trial
400
4.56
4
I mean inside the cast operator Pounds() it is showing real decimal valie 4.56 but inside main 4. Not able to figure out, what is happening.

Overloading the << operator. Where/to whom is the reference returned?

comp c;
...
cout<< c;
the overloaded operator<< function returns a reference. In this case where is it returned or who collects it. What is the purpose of returning a reference ?
class comp
{
int re,im;
public: comp(){re=0;im=0;}
comp(int a,int b){re=a;im=b;}
void show(){cout<<"\n"<<im<<"+i"<<re<<"\n\n";}
comp operator *(comp a)
{
comp temp;
temp.re=(re+a.re) - (im*a.im);
temp.im=(im*a.re) + (re*a.im);
return temp;
}
friend ostream & operator<<(ostream& dout,complex & )
};
ostream& operator << (ostream &dout, comp &a)
{
dout<< a.re;
dout<< "+i";
dout<<a.im;
return dout;
}
int main()
{
comp a(1,2),b(2,3),c;
c=a*b;
c.show();
cout<< c;
return 0;
}
A chain of << operators, like a << b << c << d, is parsed by C/C++ into (((a << b) << c) << d), which is the same as
a.operator<<(b).operator<<(c).operator<<(d)
The "result" of each << is the LHS of the next <<; thus, to make this chaining work, the ostream must be returned from each operator<<.

Use of overloaded operator '>>' is ambiguous (with operand types 'istream' (aka 'basic_istream<char>') and 'MyIncreEx')

Here is the code and I don't seem to find what's wrong with it; I need to overload the << and >> operators, but I get the following error:
Use of overloaded operator '>>' is ambiguous (with operand types 'istream' (aka 'basic_istream') and 'MyIncreEx')
I can't see what's really ambiguous about it:
class MyIncreEx;
istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);
MyIncreEx operator++(MyIncreEx& d, int dummy);
MyIncreEx operator++(MyIncreEx& d);
class MyIncreEx
{
friend istream& operator>>(istream& is, MyIncreEx s);
friend ostream& operator<<(ostream& os, MyIncreEx s);
friend MyIncreEx operator++(MyIncreEx& d, int dummy);
friend MyIncreEx operator++(MyIncreEx& d);
public:
int num1 = 0, num2 = 0, num3 = 0;
};
istream& operator>>(istream& is, MyIncreEx& s)
{
is >> s.num1;
is >> s.num2;
is >> s.num3;
return is;
};
ostream& operator<<(ostream &os, MyIncreEx& s)
{
os << "(" << s.num1 <<"," <<s.num2 << "," << s.num3 <<")"<< endl;
return os;
};
MyIncreEx operator++(MyIncreEx& d)
{
d.num1++;
d.num2++;
d.num3++;
return d;
};
MyIncreEx operator++(MyIncreEx& d, int dummy)
{
d.num1++;
d.num2++;
d.num3++;
return d;
};
int main()
{
MyIncreEx obj;
cout << "please enter three numbers: ";
cin >> obj;
cout << "The original value are: " << obj << endl;
obj++;
cout << "The new values after obj++ are: " << obj << endl;
++obj;
cout << "The new values after ++obj are: " << obj << endl;
}
You declared two different versions of the output operators:
istream& operator>>(istream& is, MyIncreEx& s);
ostream& operator<<(ostream &os, MyIncreEx& s);
class MyIncreEx
{
friend istream& operator>>(istream& is, MyIncreEx s);
friend ostream& operator<<(ostream& os, MyIncreEx s);
...
};
The friend operators have a different and conflicting signature. You probably wanted to declare them as
friend istream& operator>>(istream& is, MyIncreEx& s);
friend ostream& operator<<(ostream& os, MyIncreEx const& s);
(assuming you also fix the output operator to work with MyIncreEx const& rather than MyIncreEx&).