C++ 利用结构和函数计算直线长度

C++ 利用结构和函数计算直线长度,c++,function,struct,structure,line,C++,Function,Struct,Structure,Line,好的,所以我已经得到了一个Pseducode,并被要求遵循它,我已经做了相应的,但仍然被卡住,并得到错误的任何帮助 这是我的主要功能,(不允许更改此功能) 创建存储两点(起点和终点)的直线结构类型 创建一个基于用户输入检索点结构的函数 Point getLine() { cout << "Please enter the X, Y coordinates for the point: "; cin >> point.X >> point.Y ;

好的,所以我已经得到了一个Pseducode,并被要求遵循它,我已经做了相应的,但仍然被卡住,并得到错误的任何帮助

这是我的主要功能,(不允许更改此功能)

创建存储两点(起点和终点)的直线结构类型

创建一个基于用户输入检索点结构的函数

Point getLine()
{
    cout << "Please enter the X, Y coordinates for the point: ";
    cin >> point.X >> point.Y ;
    return point;
}
我做错了什么?? 当我运行它时,我得到了这个错误

Error   1   error C2440: 'initializing' : cannot convert from 'Point' to 'Line' 

Error   2   error C2660: 'computeLength' : function does not take 1 arguments   

您应该根据调用更改函数,如下所示:

Line getLine()
{
    Line line;
    cout << "Please enter the X, Y coordinates for the start point: ";
    cin >> line.start.X >> line.start.Y ;
    cout << "Please enter the X, Y coordinates for the end point: ";
    cin >> line.end.X >> line.end.Y ;
    return line;
}

double computeLength(const Line& line)
{
    double D;
    D = sqrt(pow(line.end.X - line.start.X, 2) + pow(line.end.Y - line.start.Y, 2));
    return D;
}
Line getLine()
{
线条;
cout>line.start.X>>line.start.Y;
cout>line.end.X>>line.end.Y;
回流线;
}
双计算长度(常数线和线)
{
双D;
D=sqrt(pow(line.end.X-line.start.X,2)+pow(line.end.Y-line.start.Y,2));
返回D;
}
约阿希姆是对的

错误1是由
行line1=getLine(…)
引起的。这是因为尽管你给它起了个名字,getLine还是得到了一分。不要改变那条线。更改getLine的定义以返回一行

Line getLine()
{
    Line line;
    Point point1;
    Point point2;

    cout << "Please enter the X, Y coordinates for the first point: ";
    cin >> point1.X >> point1.Y ;
    line.start = point1;

    cout << "Please enter the X, Y coordinates for the second point: ";
    cin >> point2.X >> point2.Y ;
    line.end = point2;

    return line;
}

我们无法真正知道您做错了什么,除非您告诉我们在运行代码时会发生什么。它不能建造吗?如果是,错误是什么?当你运行它时它会崩溃吗?当然可以,然后在调试器中运行。它是否输出了错误的值?如果是,那么告诉我们一些输入的预期和实际输出。但是,首先,你的
getLine
函数没有得到一行,而是一个点。我在上面编辑了它,要显示错误,请阅读函数的声明,然后查看您是如何使用它们的。不允许我更改主函数。如果我这样做,则会出现错误2错误C2228:“.start”的左侧必须有class/struct/unionTry,并使用
添加到getLine的顶部。变量在声明后工作得更好。
double computeLength(const Point& pt1, const Point& pt2)
{
    double D;
    D = sqrt(pow(pt1.X - pt2.X, 2) + pow(pt1.Y - pt2.Y, 2));
    return D;
}
Error   1   error C2440: 'initializing' : cannot convert from 'Point' to 'Line' 

Error   2   error C2660: 'computeLength' : function does not take 1 arguments   
Line getLine()
{
    Line line;
    cout << "Please enter the X, Y coordinates for the start point: ";
    cin >> line.start.X >> line.start.Y ;
    cout << "Please enter the X, Y coordinates for the end point: ";
    cin >> line.end.X >> line.end.Y ;
    return line;
}

double computeLength(const Line& line)
{
    double D;
    D = sqrt(pow(line.end.X - line.start.X, 2) + pow(line.end.Y - line.start.Y, 2));
    return D;
}
Line getLine()
{
    Line line;
    Point point1;
    Point point2;

    cout << "Please enter the X, Y coordinates for the first point: ";
    cin >> point1.X >> point1.Y ;
    line.start = point1;

    cout << "Please enter the X, Y coordinates for the second point: ";
    cin >> point2.X >> point2.Y ;
    line.end = point2;

    return line;
}
double computeLength(const Line& line)
{
    double D;
    Point pt1 = line.start;
    Point pt2 = line.end;
    D = sqrt(pow(pt1.X - pt2.X, 2) + pow(pt1.Y - pt2.Y, 2));
    return D;
}