I get weird notification that I'm using private members of class - which is entirely valid, but I though that I'm allowed to do so, since I did say that the method I'm using is a friendly one.
Take a look at this:
#include <iostream>
using namespace std;
class complex {
private:
double Re, Im;
public:
complex(): Re(0.0), Im(0.0){}
complex(double Re, double Im): Re(Re), Im(Im){}
double getRe() const { return Re; }
double getIm() const { return Im; }
friend complex operator+(const complex&, const complex&);
friend ostream& operator<<(ostream&, const complex&);
friend istream& operator>>(istream &, const complex &); // FRIENDLY FUNCTION
};
complex operator+(const complex& a, const complex& b) {
double r, i;
r = a.getRe()+ b.getRe();
i = a.getIm() + b.getIm();
return complex(r, i);
}
ostream& operator<<(ostream& out, const complex &a) {
out << "(" << a.getRe() << ", " << a.getIm() << ")" << endl;
return out;
}
istream &operator>>(istream &in, complex &c)
{
cout<<"enter real part:\n";
in>>c.Re; // ** WITHIN THIS CONTEXT ERROR **
cout<<"enter imag part: \n";
in>>c.Im; // ** WITHIN THIS CONTEXT ERROR **
return in;
}
int main(void) {
complex a, b,c;
cin >> a;
cin >> b;
c = a+b;
cout << c;
}
Should I declare some sort of setFunction within the class in order to get the values which are private ?
istream& operator>>(istream &, const complex &);
is not the same as
istream &operator>>(istream &in, complex &c);
Spot the difference left as exercise to the reader.
Related
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) {}
I am doing a homework assignment on overloading operators. This is my code:
#include<iostream>
#include<cmath>
using namespace std;
class ComplexNumber{
double real;
double imaginary;
public:
friend class Equation;
};
class Equation{
ComplexNumber *broevi;
char *znaci;
int n;
public:
Equation(){
broevi=new ComplexNumber[0];
znaci=new char[0];
n=0;
}
friend istream &operator >>(istream &in, Equation &e){
int k=1;
while(k){
char *tmp; ComplexNumber *b;
tmp=new char[e.n+1];
for(int i=0; i<e.n; i++)
tmp[i]=e.znaci[i];
b=new ComplexNumber[e.n+1];
for(int i=0; i<e.n; i++)
b[i]=e.broevi[i];
in>>e.broevi[e.n].real>>e.broevi[e.n].imaginary;
in>>tmp[e.n];
if(tmp[e.n]=='=')
break;
delete []e.znaci;
e.znaci=tmp;
delete[]e.broevi;
e.broevi=b;
++e.n;
}
return in;
}
friend class ComplexNumber;
};
This is only a part of my code.
While compiling I get these two following errors:
error: 'double ComplexNumber::imaginary' is private
error: 'double ComplexNumber::real' is private
On this line:
in>>e.broevi[e.n].real>>e.broevi[e.n].imaginary;
Can you please point out where my mistake is?
Your operator>> is a friend of Equation, so it can access private fields of Equation. But, it is not a member of Equation, and it is not a friend of ComplexNumber, so it can't access private fields of ComplexNumber.
The simplest solution is to add a friend operator>> to ComplexNumber, and then use that in Equation's operator>>:
class ComplexNumber {
double real;
double imaginary;
public:
friend istream& operator >>(istream &in, ComplexNumber &cn) {
in >> cn.real >> cn.imaginary;
return in;
}
};
class Equation {
...
public:
...
friend istream& operator >>(istream &in, Equation &e) {
...
//in >> e.broevi[e.n].real >> e.broevi[e.n].imaginary;
in >> e.broevi[e.n];
...
}
};
However, if that is not allowed for your assignment, then you can instead move your stream logic to a member method of Equation for its operator>> to call, and then that method will have access to the private fields of ComplexNumber:
class ComplexNumber {
double real;
double imaginary;
public:
friend class Equation;
};
class Equation {
...
void read(istream &in) {
// your logic here...
}
public:
...
friend istream& operator >>(istream &in, Equation &e){
e.read(in);
return in;
}
};
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);
}
In this code of operator Overloading, i don't want to write "using namespace std" instead i want to include "std::" wherever required.
After adding "std::" after cout and cin, i am still getting Errors where else to include "std::".
#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 &, const Complex &);
friend istream & operator >> (istream &, Complex &);
};
ostream & operator << (ostream &out, Complex &obj)
{
out<<obj.real<<" "<<obj.imag;
return out;
}
istream & operator >> (istream &in, const Complex &obj)
{
in>>obj.real>>obj.imag;
return in;
}
int main()
{
Complex obj;
std::cin>>obj;
std::cout<<obj;
return 0;
}
It should take input two numbers using istream operator and output two numbers using ostream operator.
add std:: to ostream and istream
They come from the headers <istream> and <ostream> and are defined in <iosfwd>
#include<iostream>
//using namespace std;
class Complex
{
private:
int real, imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
friend std::ostream& operator<<(std::ostream& out, const Complex& obj);
friend std::istream& operator>>(std::istream& in, Complex& obj);
};
std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
out << obj.real << " " << obj.imag;
return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
in >> obj.real >> obj.imag;
return in;
}
int main()
{
Complex obj;
std::cin >> obj;
std::cout << obj;
return 0;
}
(not related to the std:: problem)
You can also access your private variables outside the class without the friend declaration by using get/set member functions. Thanks to #aschepler for pointing out my mistake regarding the accessibility.
#include<iostream>
class Complex
{
private:
int real, imag;
public:
int get_real() const {
return real;
}
void set_real(int real) {
this->real = real;
}
int get_imag() const {
return imag;
}
void set_imag(int imag) {
this->imag = imag;
}
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
};
std::ostream& operator<<(std::ostream &out, const Complex &obj)
{
out << obj.get_real() << " " << obj.get_real();
return out;
}
std::istream& operator>>(std::istream &in, Complex &obj)
{
int real, imag;
in >> real >> imag;
obj.set_real(real);
obj.set_imag(imag);
return in;
}
int main()
{
Complex obj;
std::cin >> obj;
std::cout << obj;
return 0;
}
Your favourite standard library reference tells you what namespace things are in. In a tiny program like this, you can simply look up each one in turn.
Hint: they're all in std.
So that includes std::ostream and std::istream.
This is just a namespace pollution problem. The importance of it may vary between usages.
When you are just prototyping, using namespace std; is fine, as is including useless headers just in case you need something from one. When you want a super safe code that will be extensively reviewed, you want to prevent name collision and namespace pollutino, so you include in the current namespace only what is required, and you give explicit namepaces for idendifiers that are only seldom used.
The following is more of my own opinion (or more exactly the way I am used to work):
when a symbol is seldom used, I give it its explicit namespace (eg: std::cout << i;)
when a symbol is heavily used in a compilation unit, I import specifically that symbol (eg: using std::cout; ... cout << i; ... cout << j; ...)
I'm trying to overload the >> and << operators for use in a complex number class. For some reason my << and >> functions cannot access the real and imaginary parts of the complex objects even though I've made them friends of the class.
Complex.h:
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
friend std::ostream &operator<<(std::ostream &out, const Complex &c);
friend std::istream &operator>>(std::istream &, Complex &);
public:
explicit Complex(double = 0.0, double = 0.0); // constructor
Complex operator+(const Complex &) const;
Complex operator-(const Complex &) const;
#endif
Complex.cpp:
#include "stdafx.h"
#include "Complex.h"
#include <iostream>
using namespace std;
istream &operator>>(istream &input, Complex &complex) // input
{
cout << "enter real part:\n";
input >> complex.real;
cout << "enter imag part: \n";
input >> complex.imaginary;
return input;
}
std::ostream &operator<<(std::ostream &out, Complex c) //output
{
out<<"real part: "<<c.real<<"\n";
out<<"imag part: "<<c.imag<<"\n";
return out;
}
You didn't friend the same function you defined. You friended the first, and defined the second:
friend std::ostream &operator<<(std::ostream &out, const Complex &c);
std::ostream &operator<<(std::ostream &out, Complex c)
Also, Is the member named imaginary or imag?
input >> complex.imaginary;
out<<"imag part: "<<c.imag<<"\n";