blitzeus
blitzeus

Reputation: 495

overloaded operator<< outputs an address instead of data members

I'm trying to overload the operator<< but I keep getting the address as the output.

For example a Card *c = new Card(5,3);

then if I try to output the Card with my overloaded operator<<,

the address outputs instead of say '5 of Clubs'

cout << c;    //0x100100ff0  ?????????????

//Card.h

#ifndef JS_CARD_H
#define JS_CARD_H

#include <ostream>
using std::ostream;

#include <string>
using std::string;

#include <vector>
using std::vector;

namespace JS {
    class Card {

    friend ostream &operator<<(ostream &out, const Card &rhs);

    public:
        enum Suit { DIAMONDS, HEARTS, SPADES, CLUBS };
        enum Rank { ACE = 1, JACK = 11, QUEEN = 12, KING = 13 };

        Card(int rank, int suit) : rank(rank), suit(suit){}

        string getRank() const;
        string getSuit() const;
        int getRankValue() const;

        int operator+(const Card& rhs);
        void displayCard(const Card &rhs);

    private:
        int rank;
        int suit;
    };

}

#endif

//Card.cpp

ostream
&operator<<(ostream &out, const Card &rhs) {

    out << rhs.getRank() << " o f" << rhs.getSuit();

    return out;
}

string
Card::getSuit() const {

    switch (suit) {
        case SPADES:   return "Spades";   break;
        case HEARTS:   return "Hearts";   break;
        case DIAMONDS: return "Diamonds"; break;
        case CLUBS:    return "Clubs";    break;
        default:       return "";         break;
    }
}

string
Card::getRank() const {
    switch (rank) {
        case ACE:   return "Ace";   break;
        case JACK:  return "Jack";  break;
        case QUEEN: return "Queen"; break;
        case KING:  return "King";  break;
        default:
            stringstream out;
            out << rank;

            return out.str();
            break;
    }
}

//main.cpp

#include "Deck.h"
#include "Card.h"

#include <iostream>
using std::cout;

using namespace JS;

int main(int argc, const char * argv[]) {

    Card *c = new Card(5,3);
    Card *c1 = new Card(1,1);

    cout << c;      //0x100100ff0  ?????????????
    cout << '\n';   //5 of Clubs  
    c->displayCard();

   return 0;
}

Upvotes: 0

Views: 225

Answers (3)

Pierre Fourgeaud
Pierre Fourgeaud

Reputation: 14510

Your operator<< is declared as :

ostream &operator<<(ostream &out, const Card &rhs);

It takes a const reference on a Card as a second argument.

But in this code :

Card *c = new Card(5,3);

// ...
cout << c;

You are passing it a pointer to Card object.


You can solve this in two ways :

Card *c = new Card(5,3);
cout << *c;
//      ^ To dereference the pointer

Or

Card c(5, 3); // Why do you need a pointer anyway ?
cout << c;

Upvotes: 2

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

c is not a Card object. It is a pointer to a Card object. If you want to use your operator with it, you need to dereference it, like this:

cout << *c;

But then, why is c a pointer anyway? If you want a Card object, just make one. There is no reason for dynamic allocation here.

Card c(5, 3);
cout << c;

Your code has a memory leak anyway. For every new, you need a delete. Stop using new, and you don't need delete.

Upvotes: 11

Vaughn Cato
Vaughn Cato

Reputation: 64308

Since c is a pointer, you want to use

cout << *c;

Upvotes: 3

Related Questions