c++分数类。重载运算符?

发布于 2024-09-26 13:55:24 字数 2291 浏览 5 评论 0原文

我正在为学校项目制作分数课,我的大脑正在煎熬。我被告知要超载<<和>>通过friend关键字进行操作。但我为此遇到了错误。

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

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

。定义。错误 C2333:“分数::运算符 <” : 函数声明错误;跳过函数体

总共有 46 个...这是一场噩梦。

编辑:

谢谢,我解决了几乎所有的错误,但仍然有 3 个

错误 C2664: 'Fraction::Fraction(const Fraction &)' : 无法将参数 1 从 'int' 转换为 'const Fraction &' 在此语句中发生:

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

错误 C2248: 'Fraction::operator ==' : 无法访问类 'Fraction' 中声明的私有成员 错误 C2248:“分数::运算符 <” :无法访问类“Fraction”中声明的私有成员

我不明白这两个,但它发生在这些语句中:

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

谢谢!

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

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

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

错误:

错误 1 ​​错误 LNK2001:无法解析的外部符号“class Fraction __cdecl 运算符-(class Fraction const &,class Fraction const &)”(??G@YA?AV?$Fraction@H@@ABV0 @0@Z) 错误2错误LNK2001:无法解析的外部符号“class Fraction __cdecl operator+(class Fraction const &,class Fraction const &)”(??H@YA?AV?$Fraction@H@@ABV0@0@Z) 错误 3 错误 LNK2001:无法解析的外部符号“class Fraction __cdecl 运算符/(class Fraction const &,class Fraction const &)”(??K@YA?AV?$Fraction@H@@ABV0@0@Z) c :\用户\caleb jares\documents\visual studio 2010\Projects\Solution11-5\Solution11-5\Solution11-5.obj 错误 4 错误 LNK2001:无法解析的外部符号“class Fraction __cdecl 运算符*(class Fraction const &,class Fraction const &)”(??D@YA?AV?$Fraction@H@@ABV0@0@Z) 错误 5 错误 LNK2001:无法解析的外部符号“class std::basic_ostream > & __cdecl 运算符<<(class std::basic_ostream > const &,class Fraction)” (??6@YAAAV?$basic_ostream@DU ?$char_traits@D@std@@@std@@ABV01@V?$分数@H@@@Z) 错误 6 错误 LNK2001:无法解析的外部符号“class std::basic_istream > & __cdecl operator>>(class std::basic_istream > const &,class Fraction)” (??5@YAAAV?$basic_istream@DU ?$char_traits@D@std@@@std@@ABV01@V?$分数@H@@@Z) 错误 7 错误 LNK1120: 6 无法解析的外部

再次感谢您的帮助!

I am making a fraction class for a school project, and my brain is frying. I was told to overload the << and >> operators through the friend keyword. But I'm getting errors for this.

I've posted the relevant code here: http://pastebin.com/NgCABGJ2

The errors include: error C2270: '<<' : modifiers not allowed on nonmember functions (this error is for all the ones that are declared as friends)

This was at the operator< definition. error C2333: 'Fraction::operator <' : error in function declaration; skipping function body

There's 46 in all... this is a nightmare.

EDIT:

Thanks, I solved almost all the errors, but there's still 3

error C2664: 'Fraction::Fraction(const Fraction &)' : cannot convert parameter 1 from 'int' to 'const Fraction &'
Occurs at this statement:

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

error C2248: 'Fraction::operator ==' : cannot access private member declared in class 'Fraction'
error C2248: 'Fraction::operator <' : cannot access private member declared in class 'Fraction'

I don't understand these two, but it occurs at these statements:

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

Thanks!

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

I fixed the other private access errors, but now i have some reaaaaaaaally bizarre errors:

Full code: http://pastebin.com/MVrB67SR

Errors:

Error 1 error LNK2001: unresolved external symbol "class Fraction __cdecl operator-(class Fraction const &,class Fraction const &)" (??G@YA?AV?$Fraction@H@@ABV0@0@Z)
Error 2 error LNK2001: unresolved external symbol "class Fraction __cdecl operator+(class Fraction const &,class Fraction const &)" (??H@YA?AV?$Fraction@H@@ABV0@0@Z)
Error 3 error LNK2001: unresolved external symbol "class Fraction __cdecl operator/(class Fraction const &,class Fraction const &)" (??K@YA?AV?$Fraction@H@@ABV0@0@Z) c:\Users\caleb jares\documents\visual studio 2010\Projects\Solution11-5\Solution11-5\Solution11-5.obj
Error 4 error LNK2001: unresolved external symbol "class Fraction __cdecl operator*(class Fraction const &,class Fraction const &)" (??D@YA?AV?$Fraction@H@@ABV0@0@Z)
Error 5 error LNK2001: unresolved external symbol "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > const &,class Fraction)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@ABV01@V?$Fraction@H@@@Z)
Error 6 error LNK2001: unresolved external symbol "class std::basic_istream > & __cdecl operator>>(class std::basic_istream > const &,class Fraction)" (??5@YAAAV?$basic_istream@DU?$char_traits@D@std@@@std@@ABV01@V?$Fraction@H@@@Z)
Error 7 error LNK1120: 6 unresolved externals

Again, thanks for help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一场春暖 2024-10-03 13:55:24

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

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

将函数完全放在类块之外。您的助教不可能不经常使用 friend 来抱怨您的设计。

Sounds like you tried to declare friend ostream &operator<<(…) const; . The important thing about friends is that they are not members. A friend function exists outside the scope of the class, even if it is defined inside the class {} block. In other words, you are declaring a function ::operator<<(), not fraction::operator<<(). And only member functions can have that trailing const, since it modifies the type of this.

Fact is, operator<< for output usually shouldn't be a friend anyway. It just gets the value and forwards it to the stream… that shouldn't require any special permission! Same applies to operator<.

Take the functions outside the class block entirely. There's no way your TA can complain about your design using friend less often.

静待花开 2024-10-03 13:55:24

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

Just take the const off... they're not member functions, so they can't be const. You should pass the class object by reference too... no point making copies all the time.

孤星 2024-10-03 13:55:24

我无法弄清楚您的第一个新错误,但是 operator==operator< 错误是因为它们是内部的 private默认为您的班级。您需要在它们前面添加一个 public: 行,以便外界可以访问它们。

I can't figure out the first of your new errors, but the operator== and operator< errors are because they're private inside your class by default. You need a public: line in front of them so they're accessible to the outside world.

潜移默化 2024-10-03 13:55:24

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

The modifiers it's referring is the const at the end of the function, after the parameters:

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

The const modifier indicates that the function doesn't modify the object that it belongs to. Since they aren't member functions, they don't belong to any object.

感受沵的脚步 2024-10-03 13:55:24

函数声明后面的 const 关键字仅适用于成员函数:

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

这是不允许的,因为成员函数声明末尾的 const 意味着该成员函数不会修改非成员函数。 - *this 对象的可变值。由于这是一个非成员函数,因此应用于函数声明末尾的 const 是一个错误。

另外,如果您定义 < 运算符(或其他运算符,例如 >+- 等.) 作为类的成员,它只能接受单个参数:

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

尽管正如 Potatoswatter 所指出的,您应该将此类运算符在类外部定义为自由函数。

The const keyword following the function declaration is only allowed for member functions:

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

It's not allowed because a const at the end of a member function declaration means this member function will not modify non-mutable values of the *this object. Since this is a non-member function, a const applied to the end of the function declaration is an error.

Also, if you define the < operator (or other operators like >, +, -, etc.) as a member of the class, it can only accept a single parameter:

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

Although as Potatoswatter has pointed out, you should define these kinds of operators outside the class as free functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文