-1

我无法使用重载的运算符输出 Card 对象的数据成员<<

我收到一条错误消息

“JS::operator<<(std::__1::basic_ostream >&, JS::Card const&)”,引用自:链接器命令失败,退出代码为 1(使用 -v 查看调用)

//
//Main.cpp

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

#include <iostream>
using std::cout;

using namespace JasonSteindorf;

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

    //Deck d;

    //d.shuffle();

    Card c(5,3);  //5 of Clubs
    Card c2;      //Ace of Diamonds

    cout << c;         //not working ????????  should says 5 of Clubs
    c.displayCard();   //works

    return 0;
}

// //
甲板.h

#ifndef JS_DECK_H
#define JS_DECK_H

#include <iostream>
using std::cout;

#include <vector>
using std::vector;

namespace JS {

    //forward declaration
    class Card;

    class Deck {

    public:
        Deck();
        void shuffle();

    private:
        vector<Card>cards;
    };

}

#endif

// //
甲板.cpp

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

#include <algorithm>
using std::random_shuffle;

#include <iostream>
using std::cout;

using namespace JS;

Deck::Deck() {

    cout << "A DECK OF CARDS!!!\n\n";

    for(int suit = 0; suit < 4; suit++) {
        for(int rank = 1; rank < 14; rank++) {

            cards.push_back(Card(rank, suit));
        }
        cout << '\n';
    }

}

void
Deck::shuffle() {

    random_shuffle(cards.begin(), cards.end());

    //for(int s = 0; s < 52; s++)
      //  cards.at(s)->displayCard();

    for(vector<Card>::iterator it = cards.begin(); it < cards.end(); it++) {
        Card a = *it;
        a.displayCard();
    }

}

// //
卡片.h

#ifndef JS_CARD_H
#define JS_CARD_H

#include <iostream>
using std::ostream;

#include <string>
using std::string;

#include <vector>
using std::vector;

namespace JS {
    class Card {

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

        Card();
        Card(int rank, int suit);

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

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

    private:
        int rank;
        int suit;
    };

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

#endif

// //
卡片.cpp

#include "Card.h"

#include <iomanip>
using std::setw;
using std::right;

#include <iostream>
using std::cout;

#include <sstream>
using std::stringstream;

using namespace JS;

Card::Card(): rank(1), suit(0){

    displayCard();
}

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

    displayCard();
}

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;
    }
}

int
Card::getRankValue() const {

    switch (rank) {
        case ACE:   return 1;    break;
        case JACK:
        case QUEEN:
        case KING:  return 10;   break;
        default:    return rank; break;
    }
}

int
Card::operator+(const Card& rhs) {

    return (getRankValue() + rhs.getRankValue());
}

ostream& operator <<(ostream& out, const Card& c)
{
    out << c.getRank() << " of " << c.getSuit();
    return out;
}

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

    out << rhs.getRank() << " of " << rhs.getSuit();

    return out;
}*/


void
Card::displayCard() const {

    cout << right << setw(5)   << getRank()
                  << setw(3)   << " of "
                  << getSuit() << '\n';
}
4

1 回答 1

4

Card.h在命名空间中声明运算符:

namespace JS {
    ostream& operator << (ostream& out, const Card& c);
}

Card.cpp在全局命名空间中定义了不同的运算符:

ostream& operator <<(ostream& out, const Card& c) {...}

您的选择是:

  • 将声明放在全局命名空间中;或者
  • 将定义放在JS命名空间中;或者
  • 按照定义限定名称JS::operator<<
于 2013-08-15T17:03:34.550 回答