我无法使用重载运算符<<
输出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;
}
//
//Deck.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
//
//Deck.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();
}
}
//
//Card.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
//
//Card.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';
}
答案 0 :(得分:4)
Card.h
在名称空间中声明运算符:
namespace JS {
ostream& operator << (ostream& out, const Card& c);
}
但是Card.cpp
在全局命名空间中定义了一个不同的运算符:
ostream& operator <<(ostream& out, const Card& c) {...}
您的选择是:
JS
命名空间中;或JS::operator<<
。