【发布时间】:2020-07-20 11:02:31
【问题描述】:
我正在编写代码来计算和显示二维坐标系统中的线长。但是,我的代码中有一些错误,我不确定如何修复它们。我编译时的错误如附图所示。CODING ERROR
#include<iostream>
#include<math.h>
using namespace std;
class Line
{ private:
int x1, y1, x2, y2;
public:
Line() : x1(0), y1(0), x2(0), y2(0) // default constructor
{
Line ( int a, int b, int c, int d ) // constructor
{
x1 = a, y1 = b, x2 = c, y2 = d;
};
void setLine() ; // mutator function
void getLine() const; // accessor function
double getLength() const; // returns length of a line
}
void Line :: setLine()
{ cout << "Enter x-coordinate and y-coordinate of first endpoint : ";
cin >> x1 >> y1;
cout << "Enter x-coordinate and y-coordinate of second endpoint : ";
cin >> x2 >> y2;
}
double Line :: getLine() const
{ cout << "\nThe endpoints of the line are : ";
cout << "( " << x1 << ", " << y1 << " ) and ";
cout << "( "<< x2 << ", " << y2 << " )" << endl;
}
double Line :: getLength() const
{ return sqrt ( pow(( x1 - x2), 2) + pow ((y1 - y2), 2) );
}
int main()
{ Line myline{ 2, 3, 4, 5};
myline.getLine();
cout << "Length of the line = " << myline.setLength() <<endl;
cout << "Edit the line : " << endl;
myline.setLine();
myline.getLine();
cout << "Length of the line = " << myline.getLength() <<endl;
return 0;
}
};
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读the help pages,接受SO tour,阅读How to Ask,以及this question checklist。最后请edit你的问题包括错误复制粘贴作为文本,并请在你得到错误的行上添加cmets。
-
请注意发布链接中的第一个警告。您应该首先将命令行选项
-std=c99替换为-std=c++17或类似选项。