【问题标题】:What's is wrong with this piece of c++ code? I am practising object oriented programming [closed]这段 c++ 代码有什么问题?我正在练习面向对象的编程[关闭]
【发布时间】:2011-03-17 22:11:09
【问题描述】:
#include <iostream>
#include <math.h>

using namespace std;

class Point{
public:
    Point(int xx, int yy);
    ~Point();

    int getX();
    int getY();
    void setX(int xx){ x = xx; }
    void setY(int yy){ y = yy;}

private:
    int x;
    int y;

};

Point::Point(int xx, int yy)
{
    x = xx;
    y = yy;
}

Point::~Point()
{
}

int Point::getX()
{
    return x;
}


int Point::getY()
{
    return y;
}

class Line
{
public:
    Line(Point one, Point two);
    ~Line();
    float length();

private:
    Point a;
    Point b;
};

Line::Line(Point one, Point two)
{
    a.setX = one.getX;
    a.setY = one.getY;
    b.setX = two.getX;
    b.setY = two.getY;
}

float Line::length()
{
    int x1,y1,x2,y2;
    float linelength;

    x1 = a.getX;
    y1 = a.getY;
    x2 = b.getX;
    y2 = b.getY;

    linelength = ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1));
    linelength = sqrtf(linelength);

    return linelength;
}

int main()
{
    Point mine(1,1);
    Point yours(2,2);

    Line ours(mine, yours);

    cout << Line.linelength();

    return 0;
}

【问题讨论】:

  • 请重新格式化代码。选择所有内容并按“代码”按钮。另外,请解释一下您在做什么以及遇到什么错误。
  • 有人请添加信息问题实际上是关于什么的?
  • 你认为它有什么问题?你甚至尝试编译它吗?发生了什么?为什么你认为这是错误的?你的问题是缺乏的。 -1
  • 大概是关于编译错误。

标签: c++ oop


【解决方案1】:

有一件事是这条线

cout << Line.linelength();

应该是

cout << ours.length();

【讨论】:

  • 应该是ours.length(); linelength 是局部变量,不是函数名。
【解决方案2】:

你的问题不清楚。你的意思是它不编译?

尝试正确调用函数,即

x1 = a.getX();

而不是

x1 = a.getX;

a.setX(one.getX());

而不是

a.setX = one.getX;

但可能还有更多错误......

【讨论】:

    【解决方案3】:
    a.setX = one.getX;
    a.setY = one.getY;
    b.setX = two.getX;
    b.setY = two.getY;
    

    setXgetX 是函数,而不是变量。因此,您需要这样使用它们:

    a.setX(one.getX());
    

    将来为了在这里获得帮助,您应该提供更多关于您的代码有什么问题的信息:您遇到了哪些编译器错误?什么不工作?你有什么问题?

    【讨论】:

      猜你喜欢
      • 2013-04-30
      • 2022-06-10
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多