【问题标题】:Creating 2d coordinate system programme using OOP使用 OOP 创建二维坐标系程序
【发布时间】: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 或类似选项。

标签: c++ function oop void


【解决方案1】:

你的 getLine 函数在类中定义为 void

void getLine() const; // accessor function 

你已经实现了双返回类型:你没有在类中定义

double Line :: getLine() const

将此行从 double 更改为 void 。而且您的班级检查中没有 setlength 功能。您还忘记为默认构造函数关闭 "{"";" 下课时也忘记了。额外的 ”;”在构造函数之后,这里是你的工作类

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
};

【讨论】:

  • 更改了所有这些,但代码仍然无法正常工作
【解决方案2】:

这看起来像是一个非常糟糕的尝试编写一些 C++ 代码(充满语法错误和糟糕的写作错误)。我建议您在尝试之前先学习 C++ tuto...

对于您的一段代码,这应该可以,但是如果您不明白为什么可以这样,而您的代码却没有,请去学习至少一堂 c++ 课程:

#include<iostream>
#include<math.h>
using namespace std;

class Line
{ 
private:
    int x1, y1, x2, y2;
public:
    // default constructor 
    Line() : x1(0), y1(0), x2(0), y2(0)
    {
        // empty brackets needed
    };
    
    Line ( int a, int b, int c, int d ) : x1(a), y1(b), x2(c), y2(d) // constructor
    {
        
    };

    void setLine() ; // mutator function
    void getLine() const; // accessor function
    double getLength() const; // returns length of a line
}; // need ';' after class definition

void Line::setLine()
{ 
    cout << "Enter x-coordinate and y-coordinate of first endpoint : ";
    cin >> x1; 
    cin >> y1;
    cout << "Enter x-coordinate and y-coordinate of second endpoint : ";
    cin >> x2;
    cin >> y2;
}

void Line::getLine() const  // here is void not "double". Be carefull of copy past
{
    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) );
}

// main can't be inside a class
int main()
{ 
    Line myline{ 2, 3, 4, 5};
    myline.getLine();
    cout << "Length of the line = " << myline.getLength() <<endl; // getLength, not setlength
    cout << "Edit the line : " << endl;
    myline.setLine();
    myline.getLine();
    cout << "Length of the line = " << myline.getLength() <<endl;
    return 0;
}

【讨论】:

    【解决方案3】:

    您正在调用未定义的setLength 方法,我相信您打算调用getLength。此外,您的错误消息质量低下且难以阅读。

    【讨论】:

    • 检查你的括号,他们搞砸了。
    猜你喜欢
    • 2013-09-19
    • 2016-02-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多