0

I'm trying to overloading the operator << in c++. here is my code:

both toString and the overloading of << are inside the .cpp file of VipCustomer

string VipCustomer::toString(){
    return "VIP Name: " + this->m_cuName + " Bill: "
        + to_string(this->m_cuCurrentBiil);
}

ostream& operator<< (ostream& out, VipCustomer *obj){
    return out << obj->toString();
}


int main(){
    VipCustomer * cus2 = new VipCustomer("bob", 10);
    cout << cus2 << endl;
}

The output that i'm getting is the address of cus2, what did i do wrong ?

Regarding to @T.C comments changed it to:

int main (){
    VipCustomer cus2("bob", 10);
        cout << &cus2;
}

Inside the cpp file:

string VipCustomer::toString(){
    return "VIP Name: " + this->m_cuName + " Bill: " + to_string(this->m_cuCurrentBiil);
}

ostream& operator<< (ostream& out, VipCustomer &obj){
    return out << obj.toString();
}

inside the .h file :

class VipCustomer :public Customer
{
public:
    VipCustomer();
    VipCustomer(std::string name ,int bill);
    ~VipCustomer();

    void addtoBill(int amount);
    string toString();

};

Still the same problem.

USer22999299
  • 5,284
  • 9
  • 46
  • 78

1 Answers1

2

The problem is that you use pointers. You should not use them unless it is necessary, and in your case it is not only unnecessary but also wrong. You need to change the overloaded operator from pointer to reference:

ostream& operator<< (ostream& out, VipCustomer &obj){

and the construction of your object should not use dynamic memory:

VipCustomer cus2("bob", 10);

(Even better to make the argument of the operator const reference, but then you also need to make toString const - as it should be. No need to use this-> to access members).

Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51
  • `obj` should probably be const. – T.C. Aug 04 '14 at 15:34
  • @USer22999299 Is the overloaded `operator <<`'s declaration visible in `main()`? – T.C. Aug 04 '14 at 15:37
  • @T.C. how should i do it? seems like this is my problem.. – USer22999299 Aug 04 '14 at 15:38
  • 1
    After removing pointers you cannot get the address of cus2 instead of what you want. Show your modified code, most likely you still have some problem elsewhere. – Wojtek Surowka Aug 04 '14 at 15:38
  • 1
    @USer22999299 Add it to your header file? – T.C. Aug 04 '14 at 15:38
  • Just modify the question with the changes. – USer22999299 Aug 04 '14 at 15:41
  • 1
    `cout << &cus2;` is the new problem. Why have you added completely unnecessary `&`? You output to stream the pointer and then you ask why you got the pointer. – Wojtek Surowka Aug 04 '14 at 15:42
  • while using cout << cus2 the code is not compiling . error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'VipCustomer' (or there is no acceptable conversion) – USer22999299 Aug 04 '14 at 15:44
  • 1
    Because the operator is not visible. It is not enough to add it to .cpp, you also need to declare it in .h as T.C. suggested already some time ago. – Wojtek Surowka Aug 04 '14 at 15:45
  • Ok thank you , my problem was that i was trying to declare it inside the class it self in the .h file , I could only declare it in the .h file out side the class scope , have any idea why? – USer22999299 Aug 04 '14 at 15:49
  • 1
    When you declare you overloaded operator inside the class, it has - as all member function - implicit first parameter of the type of your class. (Actually a reference but let's leave it atm). The stream operators need to have stream as the first parameter, so they cannot be class members. – Wojtek Surowka Aug 04 '14 at 15:53