【问题标题】:Overloading operators in a Rectangle Class重载 Rectangle 类中的运算符
【发布时间】:2015-05-12 02:15:03
【问题描述】:

我的程序将无法运行并给我错误消息。一开始我忘了在头文件的}后面加分号。我回去添加了一个,但 Visual Studio 不断给我错误。

错误消息链接:https://pastebin.com/wSEnedMY

#ifndef RECTANGLE_H
#define RECTANGLE_H
using namespace std; 

// Class declaration
class Rectangle
{
    private: 
        double length; 
        double width; 
    public: 
        Rectangle(); 
        Rectangle(double, double); 
        Rectangle operator-(Rectangle &);
        Rectangle operator*(Rectangle &);
        friend istream& operator>>(istream &, Rectangle &);
}; 
#endif

#include "stdafx.h" 
#include <iostream> 
#include "Rectangle.h" 
using namespace std; 

// Default Constructor
Rectangle::Rectangle()
{
    length = 0; 
    width = 0; 
}

// Constructor 
Rectangle::Rectangle(double len, double wid)
{
    length = len; 
    width = wid; 
}

// Overload the - operator 
Rectangle Rectangle::operator-(Rectangle &otherRect)
{
    Rectangle temp;

    temp.length = this->length - otherRect.length;
    temp.width = this->width - otherRect.width;

    return temp;
}

// Overload the * operator
Rectangle Rectangle::operator*(Rectangle &otherRect)
{
    Rectangle temp;

    temp.length = this->length * otherRect.length;
    temp.width = this->width * otherRect.width;

    return temp;
}

// Overload the cin operator
istream& operator>>(istream &is, Rectangle& r)
{
    // Prompt user for length
    cout << "Enter the length: ";
    is >> r.length;

    // Prompt user for width
    cout << "Enter the width: ";
    is >> r.width;

    return is;
}

#include "stdafx.h" 
#include "Rectangle.h" 
#include <iostream> 
using namespace std; 

int main()
{
    Rectangle r1(3,5);
    Rectangle r3, r4, r5, r6;
    Rectangle r2(r1);               // Copy constructor

    cin >> r2;                      // Read in value for r2 and to be overloaded
    
    r3 = r1 – r2;
    cout << r3;

    r4 = r1 * r2;
    cout << r4;

    system("PAUSE"); 

    return 0; 

这是学生 9**********。请忽略此消息,因为这是针对遇到此帖子的任何讲师的。有人建议我这样做以避免任何类型的剽窃问题。

【问题讨论】:

  • “Visual Studios 不断给我错误信息”,那么请将它们包含在您的问题中。
  • 您收到的错误是什么?
  • 如果您试图在该屏幕截图中显示错误,它们是不可读的。请复制它们并将它们编辑到您的原始问题中。 :)
  • 顶部的消息“污染”了问题中最明显的部分。请考虑将其移至底部,甚至放在 cmets 中。
  • Rectangle.h 至少应该包含&lt;istream&gt;,因为您正在使用它。

标签: c++ class operator-overloading


【解决方案1】:

你有两个问题。首先是您没有声明或定义插入运算符。你在这里使用它:

cout << r3;

cout << r4;

您的第二个问题似乎是您尝试减去两个矩形的行有一个不是减号的字符:

r3 = r1 – r2
//      ^This isn't a subtraction, it's a hyphen or something.

r3 = r1 - r2
//      ^See the difference?

在添加插入运算符重载并修复该减号后,您的代码将被编译。

【讨论】:

  • 谢谢@dwcanillas。我将如何定义插入运算符以允许它工作?最初,我必须在课程的公共部分创建一个朋友 ostream,但由于作业的规范,它被删除了。
  • @Mary 这听起来像是你需要做的。我在您发布的内容中看不到任何限制您创建插入运算符的内容。你不能cout &lt;&lt; r3(或r4)不定义一个,所以你要么需要删除那些cout调用或定义运算符。
【解决方案2】:

确保将 include fstream 添加到您的标头中,同时使所有标头都位于顶部并重载输出运算符(如果您没有)。我希望这对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2011-11-29
    相关资源
    最近更新 更多