c ++分数类。超载运营商?

时间:2010-10-07 01:29:08

标签: c++ operators operator-overloading

我正在为一个学校项目制作一个分数级别,我的大脑正在煎炸。有人告诉我重载<<和>>运营商通过friend关键字。但是我为此收到了错误。

我在此处发布了相关代码:http://pastebin.com/NgCABGJ2

错误包括:错误C2270:'<<' :非成员函数不允许使用修饰符(此错误适用于所有声明为朋友的错误)

这是在运营商<定义。错误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&lt;' :无法访问在“Fraction”类中声明的私有成员

我不明白这两个,但它出现在这些陈述中:

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

谢谢!

&LT;&GT;&LT;&GT;&LT;&GT;&GT; EDIT2&LT;&LT;&GT;&LT;&GT;&LT;&GT;

我修复了其他私有访问错误,但现在我有一些令人遗憾的奇怪错误:

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

错误:

错误1错误LNK2001:未解析的外部符号“class Fraction __cdecl operator-(class Fraction const&amp;,class Fraction const&amp;)”(?? G @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z) 错误2错误LNK2001:未解析的外部符号“class Fraction __cdecl operator +(class Fraction const&amp;,class Fraction const&amp;)”(?? H @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z) 错误3错误LNK2001:未解析的外部符号“class Fraction __cdecl operator /(class Fraction const&amp;,class Fraction const&amp;)”(?? K @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z)c :\ Users \ caleb jares \ documents \ visual studio 2010 \ Projects \ Solution11-5 \ Solution11-5 \ Solution11-5.obj 错误4错误LNK2001:未解析的外部符号“class Fraction __cdecl operator *(class Fraction const&amp;,class Fraction const&amp;)”(?? D @ YA?AV?$ Fraction @ H @@ ABV0 @ 0 @ Z) 错误5错误LNK2001:未解析的外部符号“class std :: basic_ostream&gt;&amp; __cdecl operator&lt;&lt;(class std :: basic_ostream&gt; const&amp;,class Fraction)”(?? 6 @ YAAAV?$ basic_ostream @ DU ?$ @ char_traits @ d性病性病@@@ @@ ABV01 @ V'$分数3 H @@@ Z) 错误6错误LNK2001:未解析的外部符号“class std :: basic_istream&gt;&amp; __cdecl operator&gt;&gt;(类std :: basic_istream&gt; const&amp;,class Fraction)”(?? 5 @ YAAAV?$ basic_istream @ DU ?$ @ char_traits @ d性病性病@@@ @@ ABV01 @ V'$分数3 H @@@ Z) 错误7错误LNK1120:6个未解析的外部

再次,谢谢你的帮助!

5 个答案:

答案 0 :(得分:1)

只需取消const ...它们不是成员函数,因此它们不能是const。你也应该通过引用传递类对象......没有必要一直复制。

答案 1 :(得分:1)

听起来你试图声明friend ostream &operator<<(…) const;。关于friend s的重要一点是它们不是成员friend函数存在于类的范围之外,即使它是在class {}块内定义的。换句话说,您声明的是函数::operator<<(),而不是fraction::operator<<()。并且只有成员函数可以具有尾随const,因为它修改了this的类型。

事实上,operator<<输出通常不应该是朋友。它只是获取价值并将其转发到流...这不应该需要任何特殊许可!同样适用于operator<

完全取出类块外的函数。您的技术支持人员无法使用friend来减少您的设计投诉。

答案 2 :(得分:1)

我无法弄清楚您的第一个新错误,但operator==operator<错误是因为默认情况下它们在您的类中private。你需要一条public:线,以便外面的世界可以访问它们。

答案 3 :(得分:0)

函数声明后面的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所指出的那样,你应该在课外定义这些类型的运算符作为自由函数。

答案 4 :(得分:0)

它所引用的修饰符是函数末尾的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修饰符表示该函数不会修改它所属的对象。由于它们不是成员函数,因此它们不属于任何对象。