【发布时间】: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至少应该包含<istream>,因为您正在使用它。
标签: c++ class operator-overloading