簡體   English   中英

c ++分數類。 重載運算符?

[英]c++ fraction class. overloading operators?

我正在為一個學校項目做分數課,我的大腦在煎炸。 有人告訴我通過friend關鍵字重載<<和>>運算符。 但是我為此遇到了錯誤。

我已經在此處發布了相關代碼: http : //pastebin.com/NgCABGJ2

錯誤包括:錯誤C2270:'<<':非成員函數不允許使用修飾符(此錯誤適用於所有聲明為好友的函數)

這是在operator <定義中。 錯誤C2333:'Fraction :: operator <':函數聲明中的錯誤; 跳功能體

一共有46個...這是一場噩夢。

編輯:

謝謝,我解決了幾乎所有錯誤,但仍然有3個

錯誤C2664:'Fraction :: Fraction(const Fraction&)':無法將參數1從'int'轉換為'const Fraction&',發生在以下語句:

Fraction<int> test1, test2, test3(10);

錯誤C2248:'Fraction :: operator ==':無法訪問在類'Fraction'中聲明的私有成員錯誤C2248:'Fraction :: operator <':無法訪問在類'Fraction'中聲明的私有成員

我不理解這兩個,但是它們出現在以下語句中:

    if (test1 == test2)
    cout << "\nTest1 is equal to Test2";
if (test1 < test2)
    cout << "\nTest1 is less than Test2";

謝謝!

<> <> <>> EDIT2 <<> <> <>

我修復了其他私人訪問錯誤,但現在我遇到了一些嚴重的異常錯誤:

完整代碼: http//pastebin.com/MVrB67SR

錯誤:

錯誤1錯誤LNK2001:無法解析的外部符號“類分數__cdecl運算符-(類分數const&,類分數const&)”(?? G @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z)錯誤2錯誤LNK2001:未解析的外部符號“類分數__cdecl運算符+(類分數const&,類分數const&)”(?? H @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z)錯誤3錯誤LNK2001:未解析的外部符號“類分數__cdecl運算符/(類分數const&,類分數const&)”((@ K @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z)c:\\ Users \\ caleb jares \\ documents \\視覺工作室2010 \\ Projects \\ Solution11-5 \\ Solution11-5 \\ Solution11-5.obj錯誤4錯誤LNK2001:未解析的外部符號“類Fraction __cdecl運算符*(類Fraction const&,類Fraction const&)”(?? D @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z)錯誤5錯誤LNK2001:無法解析的外部符號“類std :: basic_ostream>&__cdecl運算符<<((類std :: basic_ostream> const&,class Fraction)” (?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @ std @@@ std @@ ABV01 @ V?$ Fraction @ H @@@ Z)錯誤 或6個錯誤LNK2001:無法解析的外部符號“ class std :: basic_istream>&__cdecl運算符>>(class std :: basic_istream> const&,class Fraction)”(?? 5 @ YAAAV?$ basic_istream @ DU?$ char_traits @ D @std @@@ std @@ ABV01 @ V?$ Fraction @ H @@@ Z)錯誤7錯誤LNK1120:6個未解決的外部

再次感謝您的幫助!

只是刪除const ...它們不是成員函數,因此它們不能成為const。 您也應該通過引用傳遞類對象...一直沒有必要進行復制。

聽起來像是您試圖聲明friend ostream &operator<<(…) const; 關於friend的重要一點是他們不是會員 即使在class {}塊內定義了friend函數,該函數也位於類的范圍之外。 換句話說,您要聲明一個函數::operator<<() ,而不是fraction::operator<<() 而且只有成員函數可以具有尾隨的const ,因為它修改了this的類型。

事實是,對於輸出來說, operator<<通常不應該成為朋友。 它只是獲取值並將其轉發到流中……不需要任何特殊權限! 同樣適用於operator<

將功能完全放在類塊之外。 您的TA不可能少用friend抱怨您的設計。

我無法弄清您的第一個新錯誤,但是operator==operator<錯誤是因為默認情況下它們是在您的類中private 您需要在其前面有一個public:線路,以便外界可以訪問它們。

函數聲明后的const關鍵字僅適用於成員函數:

// This is not a member function!
template <class T>
ostream& operator<<(const ostream& output,
                    const Fraction<T>& value) /* No const! */
{
    // ...
}

因為它不允許const在成員函數聲明結尾表示該成員函數不會修改非mutable的值*this對象。 由於這是一個非成員函數,因此應用於函數聲明末尾的const會出錯。

另外,如果將<運算符(或其他運算符,例如>+-等)定義為該類的成員,則它只能接受一個參數:

// Only accepts a single parameter (the "right-hand-side" argument)
bool operator<(Fraction<T> const& right) const
{
    // ...
}

盡管正如Potatoswatter指出的那樣,您應該在類外將這些運算符定義為自由函數。

它所引用的修飾符是函數末尾的const,位於參數之后:

friend ostream& operator<<(const ostream& output, const Fraction<T> value) const; //<-- that
friend istream& operator>>(const istream& input, Fraction<T> value) const; // <-- and that

const修飾符表示該函數不會修改其所屬的對象。 由於它們不是成員函數,因此它們不屬於任何對象。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM