以下引发了编译时错误。我不确定如何调用一个对象 重载运算符<<在另一个对象中<<运营商?如果有人能指出我做错了什么,那真的很有帮助。谢谢你的时间!
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&amp; operator&lt;&lt;(std :: ostream&amp;,Hand&amp;)': hand.cpp:40:错误:'out&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; ((手*)昌德) - &GT;手:: getCard(0)” /usr/include/c++/4.2.1/ostream:112:注意:候选人是:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;(*)(std :: basic_ostream&lt; _CharT,_Traits&gt;&amp;))[with _CharT = char, _Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:121:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_ios&lt; _CharT,_Traits&gt;&amp;(*)(std :: basic_ios&lt; _CharT,_Traits&gt;&amp;))[with _CharT = char, _Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:131:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: ios_base&amp;(*)(std :: ios_base&amp;))[with _CharT = char,_ Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:169:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:173:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:177:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(bool)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:92:note:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(short int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:184:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(short unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:106:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:195:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(unsigned int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:204:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_ Traits&gt; :: operator&lt;&lt;(long long int)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:208:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long long unsigned int)[with _CharT = char,_ Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:213:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(double)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:217:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_ Traits&gt; :: operator&lt;&lt;(float)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:225:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(long double)[with _CharT = char,_Traits = std :: char_traits] /usr/include/c++/4.2.1/ostream:229:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(const void *)[with _CharT = char,_ Traits = std :: char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:120:注意:std :: basic_ostream&lt; _CharT,_Traits&gt;&amp; std :: basic_ostream&lt; _CharT,_Traits&gt; :: operator&lt;&lt;(std :: basic_streambuf&lt; _CharT,_Traits&gt; *)[with _CharT = char,_Traits = std :: char_traits] hand.cpp:4:注意:std :: ostream&amp;运算符&lt;&lt;(std :: ostream&amp;,Card&amp;) hand.cpp:38:注意:std :: ostream&amp; operator&lt;&lt;(std :: ostream&amp;,Hand&amp;)
关于我遗失的任何想法?
答案 0 :(得分:4)
您的operator <<
函数应该通过const引用获取右侧参数。它告诉你它不能将getCard
返回的临时值绑定到运算符的非const引用参数。
令人难以置信的长错误消息是因为在任何时候范围内都有<<
非常多的重载,并且编译器将所有这些重载列为“我试过这个但是它不起作用。“