以下引发编译时错误。我不知道如何调用一个对象 重载运算符< <另一个对象内< <运算符?如果有人能指出我做错了什么,那会非常有帮助。感谢您的时间!在C++中调用成员的重载<<运算符
card.cpp:
#include "card.h"
#include <iostream>
ostream& operator<<(ostream &out, Card &cCard)
{
out << cCard.getValueString()
<< " OF "
<< cCard.getSuitString()
<< "S\n";
return out;
}
string Card::getValueString()
{
Value value = getValue();
switch(value)
{
case TWO: return "TWO";
case THREE: return "THREE";
case FOUR: return "FOUR";
case FIVE: return "FIVE";
case SIX: return "SIX";
case SEVEN: return "SEVEN";
case EIGHT: return "EIGHT";
case NINE: return "NINE";
case TEN: return "TEN";
case JACK: return "JACK";
case QUEEN: return "QUEEN";
case KING: return "KING";
case ACE: return "ACE";
case NONE_VALUE: return "NONE_VALUE";
default: return "UNKNOWN VALUE";
};
}
string Card::getSuitString()
{
Suit suit = getSuit();
switch(suit)
{
case SPADE: return "SPADE";
case HEART: return "HEART";
case CLUB: return "CLUB";
case DIAMOND: return "DIAMOND";
case NONE_SUIT: return "NONE_SUIT";
default: return "UNKNOWN SUIT";
};
}
card.h
#pragma once
#include <iostream>
using namespace std;
enum Suit {
SPADE,
HEART,
CLUB,
DIAMOND,
NONE_SUIT
};
enum Value {
TWO=2,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING,
ACE,
NONE_VALUE
};
class Card
{
public:
Card() : m_suit(NONE_SUIT), m_value(NONE_VALUE) { };
Card(Suit suit, Value value) : m_suit(suit), m_value(value) { };
Value getValue() { return m_value; };
Suit getSuit() { return m_suit; };
void setValue(Value value) { m_value = value; };
void setSuit(Suit suit) { m_suit = suit; };
string getValueString();
string getSuitString();
friend ostream& operator<<(ostream &out, Card &cCard);
private:
Suit m_suit;
Value m_value;
};
hand.cpp:
#include "hand.h"
#include "card.h"
ostream& operator<<(ostream &out, Card &cCard);
Hand::Hand()
{
}
Card Hand::getCard(int cardIndex)
{
if(cardIndex == 0 || cardIndex == 1)
{
return cards[cardIndex];
}
else
{
cout << "Card index out of bounds.\n";
}
return Card(NONE_SUIT, NONE_VALUE);
}
void Hand::setCard(int cardIndex, Suit suit, Value value)
{
if(cardIndex == 0 || cardIndex == 1)
{
cards[cardIndex].setValue(value);
cards[cardIndex].setSuit(suit);
}
else
{
cout << "Card index out of bounds.\n";
}
};
ostream& operator<<(ostream &out, Hand &cHand)
{
out << cHand.getCard(0)
<< "\n"
<< cHand.getCard(1);
return out;
}
hand.h:
#pragma once
#include <iostream>
#include "card.h"
using namespace std;
class Hand
{
public:
Hand();
void setCard(int cardIndex, Suit suit, Value value);
Card getCard(int cardIndex);
friend ostream& operator<<(ostream &out, Hand &cHand);
private:
Card cards[2];
};
该错误是以下内容:
hand.cpp:在函数 '的std :: ostream的&操作者< <(标准:: ostream的&,手&)': hand.cpp:40:错误:不对应的'(运营商< <'in'out < <((Hand *)cHand) - > Hand :: getCard(0)' /usr/include/c++/4.2.1/ostream:112:note:candidates are:std: :basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> ::运营商< <(std :: basic_ostream < _CharT,_Traits>(*)(std :: basic_ostream < _CharT,_Traits> &))[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:121:note:std: :basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <(标准:: basic_ios < _CharT,_Traits> &(*)(标准:: basic_ios < _CharT,_Traits> &))[与_CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:131:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits>: :运营商< <(std :: ios_base &(*)(std :: ios_base &))[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:169:note:std :: basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <(长整型)[与_CharT =炭,_Traits =标准:: char_traits] /usr/include/c++/4.2。 1/ostream:173:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> :: operator < <(long unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:177:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> ::运算符< <(布尔型)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/bits/ ostream的。TCC:92:注:标准:: basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <(短整型)[与_CharT =炭,_Traits =标准:: char_traits] /USR /include/c++/4.2.1/ostream:184:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> :: operator < <(short unsigned int)[with _CharT = char, _Traits = std :: char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:106:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> ::运算符< <(int)[with _CharT = char,_T raits =标准:: char_traits] /usr/include/c++/4.2.1/ostream:195:注:标准:: basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <( unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:204:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT ,_Traits> :: operator < <(long long int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:208:note:std :: basic_ostream < _CharT ,_Traits> & std :: basic_ostream < _CharT,_Traits> :: operator < <(long long unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:213:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> ::运算符< <(double)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:217:note:std :: basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <(浮点)[与_CharT =炭,_Traits =标准:: char_traits] /usr/include/c++/4.2.1/ostream :225:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> ::运算符< <(long double)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:229:note:的std :: basic_ostream < _CharT,_Traits> &的std :: basic_ostream < _CharT,_Traits> ::运算< <(常量无效*)[与_CharT =炭,_Traits =标准:: char_traits] 的/ usr /包括/ C++/4.2.1/bits/ostream.tcc:120:note:std :: basic_ostream < _CharT,_Traits> & std :: basic_ostream < _CharT,_Traits> :: operator < <(std :: basic_streambuf < _CharT,_Traits> *) [with _CharT = char,_Traits = std :: char_traits] hand.cpp:4:注意:标准:: ostream的&操作者< <(标准:: ostream的&,卡&) hand.cpp:38:注:标准:: ostream的&操作者< <(标准:: ostream的& ,手&)
关于我缺少什么的任何想法?
从我可以看到的那个看起来确定 - 所以要么是问题出在你所忽略的“...”部分,要么你没有很好地解释出了什么问题(换句话说,其他的是比“不编译”错误 - 例如你没有输出,因为你在'cout'行没有刷新 - 添加一个'endl'可以解决这个问题。 –
我猜想的最可能的事情是在类之后缺少'std ::'s,缺少标题或者没有分号:http://coliru.stacked-crooked.com/view?id=ccead1b951d7ebef1db70f3d9bc387f5-3b440a87a52fe2ae7c853c82f4c5144f – chris
Don'在这里看到很多东西。你在类定义的末尾忘了';'? 'info'不是'A'的成员?你真的使用'...'? – aschepler