在 C++ 中调用成员的重载 << 运算符

Posted

技术标签:

【中文标题】在 C++ 中调用成员的重载 << 运算符【英文标题】:Calling member's overloaded << operator in C++ 【发布时间】:2013-06-27 00:51:37 【问题描述】:

以下会引发编译时错误。我不确定如何调用一个对象 重载运算符

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& operatorHand::getCard(0)' 中的'operator& std::basic_ostream<_chart _traits>::operator& (*)(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>&)) [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::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator& std::basic_ostream<_chart _traits>::operator*) [其中 _CharT = char, _Traits = std::char_traits] hand.cpp:4: 注意:std::ostream& operator

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

【问题讨论】:

据我所见,这看起来不错 - 所以问题出在您遗漏的“...”部分中,或者您没有很好地解释出了什么问题(在其他换句话说,除了“它无法编译”之外,还有其他问题 - 例如,您没有输出,因为您在 cout 行上没有刷新 - 添加 endl 可能会解决这个问题。 我能猜到的最有可能是缺少std::s、缺少标题或类后没有分号:coliru.stacked-crooked.com/… 这里看的不多。您忘记了类定义末尾的;info 不是 A 的成员?你真的使用了... 您的假代码无法帮助我们诊断您的问题。请提供您的实际代码。全部。 【参考方案1】:

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

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

【讨论】:

以上是关于在 C++ 中调用成员的重载 << 运算符的主要内容,如果未能解决你的问题,请参考以下文章

C++语法小记---运算符重载

实现重载运算符'<<'时出错 - C++

C++,如何在派生类中调用基类的重载提取运算符?

类中的函数重载

在C++中,啥是运算符重载?啥是虚函数?

c++中为啥赋值运算符重载返回类型是引用