簡體   English   中英

C ++運算符重載。 從過載中獲取私有價值的功能

[英]c++ operator overloading. Function to get private value from overload

我在此類中無法獲得“打印”功能以獲取正確的總數

class PRN {
private:
typedef pair < string, int > P;   
int sz, // map size – no of distinct words
cnt, // counter for printing
total; // no of words
public:
// constructor
PRN ( const int& s = 1, const int& c = 0, const int& t = 0 ){
    cnt= c;
    total = t;
        sz = s;        
}
void operator ( ) ( const P& p ){
    total += p.second;
    cout << total;
}// overloaded operator, where P is defined as
// typedef pair < string, int > P;

void print () const{
    cout <<"no of words in output list : " << total << endl;
}

};

然后我打電話給我

PRN p (m.size());
    for_each(m.begin(),m.end(),p);
    p.print();

m是包含一些值(字符串,整數)的映射; 運算符之所以添加是因為我正在打印它們,並且可以看到它們正在被添加,但是當我調用p.print()時,它的“總計”返回零。

有什么建議么? 謝謝

問題在於for_each的行為-標准不保證for_each在執行工作時不會在內部復制p 結果,具有狀態的類似函數的對象通常無法與標准庫函數配合使用。 就我個人而言,我一直認為這種行為很奇怪,並且破壞了具有類似函數的對象的很多意義,但這就是我們要解決的問題。

根據使用方式的不同,通過使total成為PRN的靜態成員,您也許能夠或多或少地獲得所需的行為,從而所有PRN對象使用的任何地方都只有一個total值。 缺點是,在未來for_each會拿起你離開,而不是從頭開始了,但你或許能夠做這樣具有的東西來緩和PRN構造復位total假設為零(即PRN對象不壽命很長,並且創建位置與使用位置非常接近)。

另一種可能性是讓PRN對象包含一個指向另一個實際包含總數的對象的指針,如下所示:

class PRN
{
private:
    struct Storage
    {
        int total;
    };

    Storage *s;
public:
    PRN():
        s(new Storage)
    {
    }

    ~PRN()
    {
        delete s;
    }

    void operator()(const P& p)
    {
        s->total += p.second;
        ...
    }

    ...
};

使用這種方法, for_each復制p都沒有關系,因為該副本將指向與原始對象相同的Storage對象。

我是 c++ 的新手,在 &lt; <operator overloading. could you tell me some advise?< div><div id="text_translate"><p> 在自己做一些代碼練習時,我遇到了一些錯誤</p><p><strong>學生.h</strong>文件</p><pre>#pragma once class Student { public: Student() = default; Student(int id, const char* name, int score); Student(const Student&amp; s); Student(Student&amp;&amp; other); Student&amp; operator=(Student&amp;&amp; other); virtual ~Student(); **friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, const Student &amp; rhs);** void print(); private: int mId; char* mName; int mScore; size_t mSize; };</pre><p> <strong>學生.cpp</strong>文件</p><pre>#include "student.h" #include &lt;iostream&gt; #include &lt;string&gt;... //bunch of constructors, override... std::ostream &amp; operator&lt;&lt;(std::ostream&amp; os, const Student &amp; rhs) { os &lt;&lt; rhs.mId &lt;&lt; rhs.mName &lt;&lt; rhs.mScore; // Compile Error: Cannot Access Student Member(rhs.mId, rhs.mName...) return os; }</pre><p> 我得到的錯誤是我無法訪問學生 class 成員,盡管我在 student.h 中聲明它是朋友 function</p><p> 對我來說很奇怪,如果我像這樣在student.cpp文件中聲明#include&lt;iostream&gt;語句,它編譯成功</p><pre>#include &lt;iostream&gt; //change position &lt;iostream&gt; and "student.h" #include &lt;string&gt; #include "student.h"... //bunch of constructors, override... std::ostream &amp; operator&lt;&lt;(std::ostream&amp; os, const Student &amp; rhs) { os &lt;&lt; rhs.mId &lt;&lt; rhs.mName &lt;&lt; rhs.mScore; //Compile successfully return os; }</pre><p> 那么,為什么#include 語句序列很重要? 我認為#include 語句只是復制粘貼到我的 cpp 文件中。</p><p> 當我向 student.h 聲明#include&lt;iostream&gt;時,它也編譯成功,</p><p> 所以當我包含一些#include 的東西時,最好的情況是將它聲明為頭文件?</p><p> 你能給我一些建議嗎?</p><p> 我在 Visual Studio 2019 中編譯了我的程序。</p></div></operator>

[英]I'm newbie in c++, got unexpected error while <<operator Overloading. Could you tell me some advise?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 從帶有操作符重載的模板化類繼承。 “運算符*”的模棱兩可的重載 C ++箭頭運算符重載。 如何獲得訪問方法的名稱? C ++運算符重載-調用函數以在重載內獲取值 C ++重載:運算符的重載= C ++重載operator =獲得右手和左手過載 使用C ++運算符中的朋友功能無法訪問私有成員 我是 c++ 的新手,在 &lt; <operator overloading. could you tell me some advise?< div><div id="text_translate"><p> 在自己做一些代碼練習時,我遇到了一些錯誤</p><p><strong>學生.h</strong>文件</p><pre>#pragma once class Student { public: Student() = default; Student(int id, const char* name, int score); Student(const Student&amp; s); Student(Student&amp;&amp; other); Student&amp; operator=(Student&amp;&amp; other); virtual ~Student(); **friend std::ostream &amp; operator&lt;&lt;(std::ostream &amp; os, const Student &amp; rhs);** void print(); private: int mId; char* mName; int mScore; size_t mSize; };</pre><p> <strong>學生.cpp</strong>文件</p><pre>#include "student.h" #include &lt;iostream&gt; #include &lt;string&gt;... //bunch of constructors, override... std::ostream &amp; operator&lt;&lt;(std::ostream&amp; os, const Student &amp; rhs) { os &lt;&lt; rhs.mId &lt;&lt; rhs.mName &lt;&lt; rhs.mScore; // Compile Error: Cannot Access Student Member(rhs.mId, rhs.mName...) return os; }</pre><p> 我得到的錯誤是我無法訪問學生 class 成員,盡管我在 student.h 中聲明它是朋友 function</p><p> 對我來說很奇怪,如果我像這樣在student.cpp文件中聲明#include&lt;iostream&gt;語句,它編譯成功</p><pre>#include &lt;iostream&gt; //change position &lt;iostream&gt; and "student.h" #include &lt;string&gt; #include "student.h"... //bunch of constructors, override... std::ostream &amp; operator&lt;&lt;(std::ostream&amp; os, const Student &amp; rhs) { os &lt;&lt; rhs.mId &lt;&lt; rhs.mName &lt;&lt; rhs.mScore; //Compile successfully return os; }</pre><p> 那么,為什么#include 語句序列很重要? 我認為#include 語句只是復制粘貼到我的 cpp 文件中。</p><p> 當我向 student.h 聲明#include&lt;iostream&gt;時,它也編譯成功,</p><p> 所以當我包含一些#include 的東西時,最好的情況是將它聲明為頭文件?</p><p> 你能給我一些建議嗎?</p><p> 我在 Visual Studio 2019 中編譯了我的程序。</p></div></operator> 如何在C ++中適當地將&gt;運算符重載為模板函數? 學習模板重載 用於操作員過載的專用功能 向量函數 C++ 的運算符重載
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM