简体   繁体   English

C ++故障重载操作符 - 类分配

[英]C++ Trouble Overloading Operators - Class Assignment

I've viewed multiple topics on stackoverflow and I'm not getting anywhere with this class assignment. 我已经在stackoverflow上查看了多个主题,而且我没有得到任何关于此类作业的内容。 I believe I'm using the code as it was presented in the book but I'm having problems with the = operator not copying and the - operator returning two values concatenated. 我相信我正在使用本书中提供的代码,但我遇到的问题是=运算符没有复制,而运算符返回两个连接的值。 I'm hoping you can help me understand and point me in the right direction on this. 我希望你能帮助我理解并指出我正确的方向。 Any help is appreciated. 任何帮助表示赞赏。

The rectangleType class has two protected members, length and width, and a function called rectangleType.area() that multiplies them. rectangleType类有两个受保护的成员,长度和宽度,以及一个名为rectangleType.area()的函数,它们将它们相乘。 In the assignment, I'm supposed to change book code that returns length and width and return area instead but I can't get these to work right. 在作业中,我应该更改返回长度和宽度以及返回区域的书籍代码,但我无法使这些工作正常。 (Relational and stream operators are working fine.) (关系和流操作符正常工作。)

From rectangleType.h: 来自rectangleType.h:

rectangleType operator=(const rectangleType&) const; // replace one rectangle with another
rectangleType operator-(const rectangleType&) const; // subtract one rectangle from another

From rectangleTypeImp.cpp 来自rectangleTypeImp.cpp

rectangleType rectangleType::operator=(const rectangleType& rectangle) const
{
    rectangleType temp = *this;

    temp.length = rectangle.length;
    temp.width = rectangle.width;

    return temp;
}
rectangleType rectangleType::operator-(const rectangleType& rectangle) const
{
    rectangleType temp = *this;

    if(temp.length - rectangle.length >= 1 && temp.width - rectangle.width >= 1)
    {
        temp.length = temp.length - rectangle.length;
        temp.width = temp.width - rectangle.width;

        return temp;
    }
    else
    {
        cout << endl << "Dimensions are not large enough."
             << "Cancelling operation and returning dimensions"
             << "of left operand." << endl;
    }

    return temp;    
}

In the main file, I have created the following objects: 在主文件中,我创建了以下对象:

rectangleType myOtherYard(26, 19);
rectangleType myBrothersYard(2, 2);
rectangleType myMothersYard(3, 3);

and written this code: 并编写此代码:

myOtherYard = myBrothersYard;
cout << endl << "myOtherYard = myBrothersYard: " 
     << myOtherYard;
cout << endl << "myBrothersYard - myMothersYard: " 
     << myBrothersYard + myMothersYard;

Here's the output I get (using formatted printing): 这是我得到的输出(使用格式化打印):

myOtherYard = myBrothersYard:   26.0019.00 
myBrothersYard - myMothersYard: 3.003.00

It looks like there is no assignment going on in the = operator and it's returning the length and width of the first object without change. 看起来在=运算符中没有赋值,它返回第一个对象的长度和宽度而没有改变。 Also, the - operator seems to be doing its job but returning length and width separately and I don't know how to get it to return the area. 此外, - 操作员似乎正在做它的工作,但分别返回长度和宽度,我不知道如何让它返回该区域。 Everything I've tried in code has failed. 我在代码中尝试过的所有内容都失败了。

The + operator adds and returns separate length and width values. +运算符添加并返回单独的长度和宽度值。 It does look like it's adding them correctly. 它确实看起来正确添加它们。

Any way you can help me understand how to fix this? 你有什么方法可以帮我理解如何解决这个问题?

First, your assignment operation should return a reference to yourself. 首先,您的赋值操作应该返回对您自己的引用。 It should not be const either, since you are assigning to yourself. 它也不应该是常数,因为你要分配给自己。 See many references, mainly Scott Meyers "Effective C++", for reasoning. 有许多参考资料,主要是Scott Meyers的“Effective C ++”,用于推理。

Since you are just learning, and I don't see it in there, I assume you are not supposed to know about move semantics, so I'll leave that out... 由于你只是在学习,我没有在那里看到它,我认为你不应该知道移动语义,所以我会把它留下......

Assignment operator from another rectangle... 另一个矩形的赋值运算符...

rectangleType &
rectangleType::
operator=(rectangleType const & that)
{
    length = that.length;
    width = that.width;

    return *this;
}

However, it looks like the default assignment operator would be sufficient. 但是,看起来默认的赋值运算符就足够了。 If you are required to write the assignment operator for class, I imagine you also have to write the copy constructor as well. 如果您需要为类编写赋值运算符,我想您也必须编写复制构造函数。

If you are going to have an operator- you should also have operator-= . 如果你想要一个operator-你也应该有operator-= Again, consult Meyers for lots of explanation. 再次,请咨询Meyers以获得大量解释。

On your subtraction implementation... Egads. 关于你的减法实现...... Egads。 Really? 真? cout for an error? cout的错误? and then return an object that is not the right answer? 然后返回一个不正确答案的对象?

Something like that may help: 这样的事可能会有所帮助:

rectangleType& rectangleType::operator=(const rectangleType& rectangle) 
{
    if (this != &rectangle)
    {
         length = rectangle.length;
         width = rectangle.width;
    }

    return *this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM