久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

'朋友'功能和<<運(yùn)算符重載:為類

#39;friend#39; functions and lt;lt; operator overloading: What is the proper way to overload an operator for a class?(朋友功能和lt;lt;運(yùn)算符重載:為類重載運(yùn)算符的正確方法是什么?) - IT屋-程序員軟件開發(fā)技術(shù)分享
本文介紹了'朋友'功能和<<運(yùn)算符重載:為類重載運(yùn)算符的正確方法是什么?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在我正在處理的一個(gè)項(xiàng)目中,我有一個(gè) Score 類,在下面的 score.h 中定義.我試圖重載它,所以當(dāng)對(duì)它執(zhí)行 << 操作時(shí),會(huì)打印 _points + " " + _name .

In a project I'm working on, I have a Score class, defined below in score.h. I am trying to overload it so, when a << operation is performed on it, _points + " " + _name is printed.

這是我嘗試做的:

ostream & Score::operator<< (ostream & os, Score right)
{
    os << right.getPoints() << " " << right.scoreGetName();
    return os;
}

以下是返回的錯(cuò)誤:

score.h(30) : error C2804: binary 'operator <<' has too many parameters

(這個(gè)錯(cuò)誤實(shí)際上出現(xiàn)了4次)

(This error appears 4 times, actually)

我設(shè)法通過將重載聲明為友元函數(shù)來使其工作:

I managed to get it working by declaring the overload as a friend function:

friend ostream & operator<< (ostream & os, Score right);

并從 score.cpp 中的函數(shù)聲明中刪除 Score::(實(shí)際上并未將其聲明為成員).

And removing the Score:: from the function declaration in score.cpp (effectively not declaring it as a member).

為什么這行得通,而前一段代碼卻行不通?

Why does this work, yet the former piece of code doesn't?

感謝您的時(shí)間!

編輯

我刪除了頭文件中所有對(duì)重載的提及...但我收到以下(也是唯一的)錯(cuò)誤.二進(jìn)制'<<': 沒有找到使用Score"類型的右側(cè)操作數(shù)的運(yùn)算符(或者沒有可接受的轉(zhuǎn)換)為什么我的測(cè)試在 main() 中找不到合適的重載?(這不是包含,我檢查過)

I deleted all mentions to the overload on the header file... yet I get the following (and only) error. binary '<<' : no operator found which takes a right-hand operand of type 'Score' (or there is no acceptable conversion) How come my test, in main(), can't find the appropriate overload? (it's not the includes, I checked)

以下是完整的分?jǐn)?shù).h

#ifndef SCORE_H_
#define SCORE_H_

#include <string>
#include <iostream>
#include <iostream>

using std::string;
using std::ostream;

class Score
{

public:
    Score(string name);
    Score();
    virtual ~Score();
    void addPoints(int n);
    string scoreGetName() const;
    int getPoints() const;
    void scoreSetName(string name);
    bool operator>(const Score right) const;

private:
    string _name;
    int _points;

};
#endif

推薦答案

注意:您可能想查看 運(yùn)算符重載常見問題解答.

二元運(yùn)算符可以是其左側(cè)參數(shù)類的成員,也可以是自由函數(shù).(某些運(yùn)算符,如賦值,必須是成員.)由于流運(yùn)算符的左側(cè)參數(shù)是一個(gè)流,因此流運(yùn)算符要么必須是流類的成員,要么是自由函數(shù).為任何類型實(shí)現(xiàn) operator<< 的規(guī)范方法是:

Binary operators can either be members of their left-hand argument's class or free functions. (Some operators, like assignment, must be members.) Since the stream operators' left-hand argument is a stream, stream operators either have to be members of the stream class or free functions. The canonical way to implement operator<< for any type is this:

std::ostream& operator<<(std::ostream& os, const T& obj)
{
   // stream obj's data into os
   return os;
}

注意它不是一個(gè)成員函數(shù).另請(qǐng)注意,它需要對(duì)象按 const 引用進(jìn)行流式處理.那是因?yàn)槟幌霃?fù)制對(duì)象以流式傳輸它,并且您也不希望流式傳輸更改它.

Note that it is not a member function. Also note that it takes the object to stream per const reference. That's because you don't want to copy the object in order to stream it and you don't want the streaming to alter it either.

有時(shí)您希望流式傳輸內(nèi)部無法通過其類的公共接口訪問的對(duì)象,因此操作員無法獲取它們.那么你有兩個(gè)選擇:要么將一個(gè)公共成員放入進(jìn)行流式傳輸?shù)念愔?em class="showen">

Sometimes you want to stream objects whose internals are not accessible through their class' public interface, so the operator can't get at them. Then you have two choices: Either put a public member into the class which does the streaming

class T {
  public:
    void stream_to(std::ostream&) const {os << obj.data_;}
  private:
    int data_;
};

并從操作員那里調(diào)用:

inline std::ostream& operator<<(std::ostream& os, const T& obj)
{
   obj.stream_to(os);
   return os;
}

或者讓操作員成為朋友

class T {
  public:
    friend std::ostream& operator<<(std::ostream&, const T&);
  private:
    int data_;
};

以便它可以訪問類的私有部分:

so that it can access the class' private parts:

inline std::ostream& operator<<(std::ostream& os, const T& obj)
{
   os << obj.data_;
   return os;
}

這篇關(guān)于'朋友'功能和&lt;&lt;運(yùn)算符重載:為類重載運(yùn)算符的正確方法是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 亚洲国产精品一区二区第一页 | 性色在线 | 国产高清一区二区三区 | 91精品国产91久久久久久吃药 | 精品一区欧美 | 久草免费视 | 一级片视频免费 | 免费久久久久久 | 一级在线免费观看 | 日韩a v在线免费观看 | 夜夜爽99久久国产综合精品女不卡 | 中文字幕一区二区三区在线观看 | 成人精品 | 免费一区二区三区 | av网站在线看 | 亚洲免费成人av | 欧美精品一区二区三区蜜桃视频 | 欧美视频偷拍 | 日本在线视频中文字幕 | 免费同性女女aaa免费网站 | 热re99久久精品国99热观看 | 自拍偷拍精品 | 99一区二区 | 亚洲九九 | 久久精品视频在线免费观看 | 免费同性女女aaa免费网站 | 黄色日本片 | 草草视频在线免费观看 | 久久精品免费一区二区 | 欧美激情视频一区二区三区在线播放 | 国产精品一区二区三区久久 | 一区二区不卡视频 | 国产精品一区二区av | 激情 亚洲 | 波多野结衣精品在线 | 色婷婷av一区二区三区软件 | 国产精品永久免费 | 99久久国产精| 精品免费在线 | 日日摸夜夜添夜夜添精品视频 | 久久精品天堂 |