#Private members not accessible

35 messages · Page 1 of 1 (latest)

vernal ridge
#

Hello everyone I am trying to learn the basics of classes and operator overloading. And I am getting the following error. Here is my code.

dull fossilBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

vernal ridge
obtuse cedar
#

Do make em friend ie:

Add at your class body in the bottom:

friend ifstream & operator>>(..;
friend ofstream & operator<<(..;

#

I omitted the full declaration

#

I see now you have actually done this - so I'm looking

#

How about you add :: before in the friend declarations

#

like friend ifstream & ::operator>>

vernal ridge
#

This doesn't work either

amber briar
#

In the operator >>, the complex parameter shouldn't be const, it's modified.
And in the header, you declared the operator << with the wrong return type complex => ostream &.

vernal ridge
#

ok

#

Can I upload the files over here so that if anyone online can fix it?

#

Thanks

dull fossilBOT
#

@vernal ridge Has your question been resolved? If so, run !solved :)

lilac swift
#

try istream and ostream instead of ifstream and ofstream

#

@vernal ridge

vernal ridge
#

My brain has got fried... got stuck in it for hours and did some few tries. Then got busy with something else

vernal ridge
#

really appreciate your help

lilac swift
vernal ridge
#

ok lemme try now

lilac swift
#

it worked for me

vernal ridge
#

Coz will be heading out soon

vernal ridge
lilac swift
#
    friend istream & operator>>(istream &is, complex &b) {
        is >> b.real;
        is >> b.imaginary;
        return is;
    }
    friend ostream & operator<<(ostream &os, const complex &b) {
        os << b.real<<"\n";
        os << b.imaginary<<"\n";
        return os;
    }
vernal ridge
#

looks good now

#

Having issues with overloading ~ operator

#

which is supposed to return the complex conjugate

lilac swift
#

hmm

#
complex complex::operator~() const {
    return {this->real, -this->imaginary};
}
lilac swift
#

should work

dull fossilBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.