C++运算符重载方法

发布于 2024-10-18 14:45:17 字数 897 浏览 1 评论 0原文

我正在做一些家庭作业,并且在如何形成用于重载类成员的方法签名方面遇到问题。

我的头文件

class MyInt
{
    int internalInt;
public:
    MyInt::MyInt(int i);
    const MyInt operator+(const MyInt& mi);
    const MyInt& operator++();
};

我的代码文件

MyInt::MyInt(int i)
{
    internalInt = i;
}

const MyInt MyInt::operator+(const MyInt& mi)
{
    cout << "Inside the operator+\n";
    mi.print(cout);
    return MyInt(internalInt + mi.internalInt);
}

const MyInt& MyInt::operator++()
{
    cout << "Inside the operator++\n";
    internalInt++;
    return this; //Line 42
}

当我尝试编译代码时,我收到一条错误,提示

ex4.cpp:42: error: invalid initialization of reference of type ‘const MyInt&’ 
from expression of type ‘MyInt* const’

我在理解如何使其工作时遇到问题,并尝试了一些方法签名。在我的教科书中,它们排列了所有重载,但我希望找出我做错了什么,而不是仅仅按照流程来编译我的代码。

谢谢!

I'm working through some home work and having problems with how to form my method signature for overloading a member of a class.

My header file

class MyInt
{
    int internalInt;
public:
    MyInt::MyInt(int i);
    const MyInt operator+(const MyInt& mi);
    const MyInt& operator++();
};

My code file

MyInt::MyInt(int i)
{
    internalInt = i;
}

const MyInt MyInt::operator+(const MyInt& mi)
{
    cout << "Inside the operator+\n";
    mi.print(cout);
    return MyInt(internalInt + mi.internalInt);
}

const MyInt& MyInt::operator++()
{
    cout << "Inside the operator++\n";
    internalInt++;
    return this; //Line 42
}

When I try to compile the code I'm getting an error that says

ex4.cpp:42: error: invalid initialization of reference of type ‘const MyInt&’ 
from expression of type ‘MyInt* const’

I'm having problems understanding how to get this working and have tried a few method signatures. In my text book they are in lining all the overloads but I was hoping to figure out what I'm doing wrong instead of just going with the flow to get my code to compile.

Thanks!

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

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

发布评论

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

评论(2

病女 2024-10-25 14:45:17

尝试:

const MyInt& MyInt::operator++()
{
    cout << "Inside the operator++\n";
    internalInt++;
    return *this;
}

您返回的是 this 指针,而不是引用,您需要取消引用它。

try:

const MyInt& MyInt::operator++()
{
    cout << "Inside the operator++\n";
    internalInt++;
    return *this;
}

You are returning the this pointer, not the reference, you need to dereference it.

自此以后,行同陌路 2024-10-25 14:45:17

首先,在operator++()中,编写return *this;而不是return this;。还要删除 const!

-

 const MyInt operator+(const MyInt& mi);

其次,将其设为 const-function,因为

 const MyInt MyInt::operator+(const MyInt& mi) const // <--- note this!

这是 const-function。没有它,您将无法添加 MyInt 的 const 对象。

在最右边写上const后,你可以这样写:

const MyInt m1(10);
MyInt m2(20);
MyInt m3 = m1 + m2 ; //m1 is const, so it can call ONLY const-function

First of all, in operator++(), write return *this; instead of return this;. Also remove the const!

-

 const MyInt operator+(const MyInt& mi);

Second, Make it const-function, as

 const MyInt MyInt::operator+(const MyInt& mi) const // <--- note this!

This is const-function. Without it, you would not be able to add const objects of MyInt.

After you write const on the right most side, you can write this:

const MyInt m1(10);
MyInt m2(20);
MyInt m3 = m1 + m2 ; //m1 is const, so it can call ONLY const-function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文