【问题标题】:C++ Creating a function to get distance between two pointsC ++创建一个函数来获取两点之间的距离
【发布时间】:2017-02-07 10:57:37
【问题描述】:

在我的程序中,我创建了一个名为Point 的构造函数,其中包含两个值。我还有一个setgetscaletranslate 函数。我正在尝试创建一个函数,该函数允许我获取对象与另一个点之间的距离。尽管有任何帮助都会很棒,但我遇到了麻烦。

#ifndef POINTMODEL
#define POINTMODEL
#define POINTDEB UG

#include <iostream>
#include <string.h>

using namespace std;

class Point {
public:
    Point(void);
    Point(double anX, double aY);
    ~Point();

    void setPoint(double anX, double aY);

    double getX();
    double getY();

    double scaleX(double theX);
    double scaleY(double theY);
    void translate(double theX, double theY);

    void distance(const Point& aPoint);

protected:
private:
    double theX;
    double theY;
};

inline Point::Point(void)
{
    theX = 1;
    theY = 1;
    cout << "\n The default constructor was called" << endl;
}

inline Point::Point(double anX, double aY)
{
    cout << "\n regular constructor called";
}

inline Point::~Point()
{
    cout << "\n the destructor was called" << endl;
}

inline void Point::setPoint(double anX, double aY)
{
    theX = anX;
    theY = aY;
}

inline double Point::getX()
{
    return theX;
}

inline double Point::getY()
{
    return theY;
}

inline double Point::scaleX(double theX)
{
    return theX;
}

inline double Point::scaleY(double theY)
{
    return theY;
}

inline void Point::translate(double offSetX, double offSetY)
{
    cout << "X is translated by : " << offSetX << endl;
    cout << "Y is translated by : " << offSetY << endl;
}

inline void Point::distance(const Point& aPoint)
{
}

#endif

Cpp 文件:

#include "Point.h"

using namespace std;

int main(void)
{
    cout << "\n main has started" << endl;

    //Point myPoint;
    Point myPoint(1, 1);

    myPoint.setPoint(1, 1);

    cout << "\n The value for X is : " << myPoint.getX() << endl;
    cout << "\n The value for Y is : " << myPoint.getY() << endl;

    cout << "\n X scaled by 2 is : " << myPoint.scaleX(2) << endl;
    cout << "\n Y scaled by 2 is : " << myPoint.scaleY(2) << endl;

    myPoint.translate(2, 3);

    cout << "\n main has finished" << endl;

    return 0;
}

【问题讨论】:

  • 你实际上并没有说出问题所在,你确定你需要 inline 关键字吗,我的意思是,如果你正在为 wii 编译,那很好,否则它可能是多余的。跨度>
  • 为什么做不到呢?你知道计算欧几里得计划中两点之间距离的算法(公式)吗?还是执行问题?请注意,寻求调试帮助的问题(“为什么这段代码不工作?”)必须包括所需的行为、特定问题或错误以及调试所需的最短代码在问题本身中重现它。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example
  • 是的,它是植入。我听说过从值中使用 get 函数,但我无法理解它。
  • inline double Point :: scaleX( double theX){ return theX; } 是可疑的,应该更像double Point :: scaleX( double x){ return m_x *= x; } 甚至是一个 void 函数,我的意思是应该返回任何东西吗?两点之间的距离类似于double Point::Mag( const Point&amp; p ) { return std::sqrt( std::pow(m_p.x - p.x, 2) + std::pow(m_p.y - p.y, 2) ); }

标签: c++ function object distance


【解决方案1】:

你需要让你的Point::getX()Point::getY() 函数const 像这样:

inline double Point::getX() const
{
    return theX;
}

如果它们不是const,当参数是const 引用时,您不能调用它们。

那么距离就是(将返回从void改为double):

double distance(const Point & aPoint) const
{
    const double x_diff = getX() - aPoint.getX();
    const double y_diff = getY() - aPoint.getY();
    return std::sqrt(x_diff * x_diff + y_diff * y_diff);
}

我故意不使用std::pow,因为指数是 2。 您还需要为std::sqrt 包含&lt;cmath&gt;

【讨论】:

    猜你喜欢
    • 2018-07-04
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2021-06-03
    • 2014-02-17
    • 2014-01-21
    • 2020-12-22
    相关资源
    最近更新 更多