Error in reload operator
-
#include #include #include using namespace std; class Complex{ public: Complex(); Complex(double); Complex(double,double); void write() const ; bool operator==(const Complex& ) const ; bool operator!=(const Complex& ) const ; Complex operator-(); friend ostream& operator<<(ostream& out,Complex& c); private: double real; double imag; }; Complex::Complex(){ real=imag=0.0; } Complex::Complex(double re){ real=re; imag=0.0; } Complex::Complex(double re,double im){ real=re; imag=im; } void Complex::write() const { cout<
Because-because-because you have two different operator << overloads; friend ostream& operator<<(ostream& out, Complex& c); ostream& operator<<(ostream& out, const Complex& c) { ... } Their prototypes are different due to the difference inside their parameter list; in first case; Complex & in second case; const Complex & So make them the same; put const in the friend declaration.
-- ===== Arman
-
Hi, Your function declaration has different prototype, one has const reference other is not const. Best Regards Raj
But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~
-
But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~
-
But after I modified the declaration of the function to const,there are two errors which says that operator<< is ambiguous added ~
No, It should work for the << operator but shows error for
Complex Complex::operator-(){ real=-real; imag=-imag; }
because it doesnot return value. -
No, It should work for the << operator but shows error for
Complex Complex::operator-(){ real=-real; imag=-imag; }
because it doesnot return value.NO~
after I delete the funtion Complex::operator-() from the class,the error is the same as before~ -
Because-because-because you have two different operator << overloads; friend ostream& operator<<(ostream& out, Complex& c); ostream& operator<<(ostream& out, const Complex& c) { ... } Their prototypes are different due to the difference inside their parameter list; in first case; Complex & in second case; const Complex & So make them the same; put const in the friend declaration.
-- ===== Arman
NO~no~no
I have modified this problembut it do noting to the error~ -
NO~
after I delete the funtion Complex::operator-() from the class,the error is the same as before~Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message
-
Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message
there are four: error C2248: 'real' : cannot access private member declared in class 'Complex' error C2248: 'imag' : cannot access private member declared in class 'Complex' error C2593: 'operator <<' is ambiguous error C2593: 'operator <<' is ambiguous
-
Hi, I simply copied your code and put in an empty project I compiles well would you post the exact error message
By the way,the two previous errors point to the location "return out<
-
NO~no~no
I have modified this problembut it do noting to the error~Chen-XuNuo wrote:
I have modified this problem...
To what? By making both the declaration and the definition the same, the compiler error (C2248) will go away.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne