-1

以下引发编译时错误。我不确定如何在另一个对象 << 运算符中调用一个对象重载运算符 <<?如果有人能指出我做错了什么,那将非常有帮助。感谢您的宝贵时间!

卡.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";
    };
}

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

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

手.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& operator<<(std::ostream&, Hand&)'中:hand.cpp:40:错误:'out <<((Hand*) 中的'operator<<'不匹配cHand)->Hand::getCard(0)' /usr/include/c++/4.2.1/ostream:112: 注意:候选者是:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits >::operator<<(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: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::basic_ios<_CharT, _Traits>& (* )(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ostream:131: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std:: char_traits] /usr/include/c++/4.2.1/ostream:169: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long int) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ostream:173: 注意: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: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream <_CharT, _Traits>::运算符<<(bool) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:92: 注意:std::basic_ostream<_CharT, _Traits>& std:: basic_ostream<_CharT, _Traits>::operator<<(short int) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ostream:184: 注意: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: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(int) [with _CharT = char, _Traits = std::char_traits] /usr /include/c++/4.2.1/ostream:195: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(unsigned int) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ostream:204: 注意: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: 注意: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: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(double) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/ostream:217: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(float) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1 /ostream:225: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(long double) [with _CharT = char, _Traits = std::char_traits] /usr /include/c++/4.2.1/ostream:229: 注意:std::basic_ostream<_CharT, _Traits>& std::basic_ostream<_CharT, _Traits>::operator<<(const void*) [with _CharT = char, _Traits = std::char_traits] /usr/include/c++/4.2.1/bits/ostream.tcc:120: 注意: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: 注意:std::ostream& operator<<(std::ostream&, Card&) hand.cpp:38: 注意:std::ostream&运算符<<(std::ostream&, Hand&)

关于我缺少什么的任何想法?

4

1 回答 1

4

您的operator <<函数应通过 const 引用获取右侧参数。它告诉您它无法将返回的临时值绑定getCard到运算符的非常量引用参数。

令人难以置信的长错误消息是因为在任何时候都有非常多的<<范围内的重载,并且编译器将所有这些重载都列为“我尝试了这个,但它没有工作”。

于 2013-06-27T16:03:20.437 回答