-1

Below is my program, which gives, following errors

c:\mystuff>cl dummy.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 16.00.30319.01 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

dummy.cpp
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xlocale(323) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc

dummy.cpp(20) : error C2143: syntax error : missing ';' before '&'

dummy.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ doe
s not support default-int

dummy.cpp(20) : error C2061: syntax error : identifier 'ostream'

dummy.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ doe
s not support default-int

dummy.cpp(20) : error C2805: binary 'operator <<' has too few parameters

dummy.cpp(20) : error C2333: 'Complex::operator <<' : error in function declarat
ion; skipping function body

dummy.cpp(32) : error C2065: 'cout' : undeclared identifier






#include<iostream> 
class Complex{
private:
    double real;
    double imag;

public:
    Complex(double real, double imag){
        this->real = real;
        this->imag = imag;
    }

    Complex operator+ (const Complex& Operand){
        double real = this->real + Operand.real;
        double imag = this->imag + Operand.imag;
        return Complex(real,imag);
    }

    ostream &operator<< (ostream  &o, Complex Operand){//line 20
        o  << Operand.real;
        o  << Operand.imag;
        return o;
    }
};

int main(){
    Complex c1(1,2);
    Complex c2(3,4);

    Complex c3 = c1 + c2;

    cout << c3;


}

My question:

1)

At line 32 cout is visible by including extern object cout from iostream, so, What is the reason for error at line 32 cout << c3?

2)

Please help me understand, the reason for error at line #20. operator<< method is just copied from the book. In addition, I would like to understand why we should pass ostream reference as first formal parameter in operator<< method. Because, In operator+ method, i used only one operand as formal parameter by using this for accessing first operand object. Can't i do the same for operator<< method by using this?

overexchange
  • 15,768
  • 30
  • 152
  • 347
  • See [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). The way you've overloaded it, it needs to be called on an instance of `Complex`. – chris Apr 20 '14 at 14:43
  • `ostream` should be `std::ostream` on line 20 and following. Line 32, `cout` should be `std::cout`. – M.M Apr 20 '14 at 14:45
  • You should have added `using namespace std;` to use cout and friends without std:: prefix. – Ashalynd Apr 20 '14 at 14:46
  • Also the definition of `operator<<` is wrong. It must be a non-member function. – M.M Apr 20 '14 at 14:46

3 Answers3

1

You have declared the operator as a member function. This means it has to operate on an instance of Complex on the LHS and has too many parameters. What you need is to make it a non-member. One way of achieving this, and giving the operator access to the non-public members of Complex, is to declare it as a friend inside the Complex class definition:

friend std::ostream& operator << (std::ostream & o, const Complex& Operand){
    o  << Operand.real;
    o  << Operand.imag;
    return o;
}

Besides that, ostream lives in the std namespace, so refer to is as std::ostream (likewise for cout elsewhere in your code.)

Note that here I also pass Operand by const reference, since you don't need a copy.

You have exactly the same problem with Complex operator+ (const Complex& Operand).

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • you answered 2nd question – overexchange Apr 20 '14 at 15:14
  • @Sham I think I answered both. But you should only be asking one question anyway. – juanchopanza Apr 20 '14 at 15:18
  • sorry juan you are right. Are we adhering to asignature of operator<< method saying it should take ostream object as first parameter, why are we not using 'this' to access ostream object, as we are calling << on ostream object cout? – overexchange Apr 20 '14 at 15:27
  • @Sham The operator should not be a member in this case (that is the main point of my answer), so there is no `this` involved. – juanchopanza Apr 20 '14 at 15:28
  • operator << should not be a member in the sense, it is not even a member of ostream class? As you say 'std::cout << c3' so i was assuming we are calling << on cout object. am i wrong here? So friend specifier method is not a member of any class? – overexchange Apr 20 '14 at 15:35
  • @Sham that is correct. `std::cout` is an `std::ostream`. – juanchopanza Apr 20 '14 at 15:38
  • one last question, 'cout << c3;' does not imply an intuition that one need to write an operator << method with ostream as first formal parameter, do u think the same? Is this blind c++ style that compiler is expecting? – overexchange Apr 20 '14 at 15:43
  • @Sham That is fine. `operator symbol (LHS, RHS)` is used as `LHS symbol RHS`. Here, `symbol` is `<<`. – juanchopanza Apr 20 '14 at 15:45
  • if you say, operator symbol(lhs, rhs), then are we actually overloading < No i dont think so? – overexchange Apr 20 '14 at 16:06
  • sorry moderator, can we dicuss this on chat? – overexchange Apr 20 '14 at 16:07
  • @Sham Yes, you're overloading `ostream& operator<<(ostream&, Complex)`, regardless of what you think :-) – juanchopanza Apr 20 '14 at 16:16
1

You have to specify name space where names cout and ostream are defined. For example

std::ostream &operator<< (std::ostream  &o, Complex Operand){//line 20
    o  << Operand.real;
    o  << Operand.imag;
    return o;
}

std::cout << c3;

Or you could include directive

using namespace std;

after the included header. Or you could use using declarations

using std::ostream;
using std::cout;

The first approach when qualified names are used is preferable.

Also operator << must be defined as a friend function

friend std::ostream &operator<< (std::ostream  &o, const Complex &Operand){//line 20
    o  << Operand.real;
    o  << Operand.imag;
    return o;
}

So the class could be defined as

class Complex{
private:
    double real;
    double imag;

public:
    Complex(double real, double imag){
        this->real = real;
        this->imag = imag;
    }

    Complex operator+ (const Complex& Operand) const {
        double real = this->real + Operand.real;
        double imag = this->imag + Operand.imag;
        return Complex(real,imag);
    }

    friend std::ostream &operator<< (std::ostream  &o, const Complex &Operand){//line 20
        o  << Operand.real;
        o  << Operand.imag;
        return o;
    }
};
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
-1

To correctly overload the << operator, you must write a function outside the class

ostream &operator<< (ostream &o, Complex Operand){
    o  << Operand.real;
    o  << Operand.imag;
    return o;
}

and then declare it as friend inside the class

friend ostream &operator<< (ostream &, Complex);

Also, i believe that you wanted to output something like this:

o << Operand.real << " + " << Operand.imag << "i";

Also, try using

using namespace std;

after the include lines as ostream and many other features of c++ are within the std namespace.

Valdrinium
  • 1,398
  • 1
  • 13
  • 28