简体   繁体   English

C++ 菜鸟:计算两点之间的距离

[英]C++ rookie: calculating distance between two points

I am trying to write some simple C++ code to output distance between points in d-dimensions.我正在尝试编写一些简单的 C++ 代码到 d 维度中点之间的 output 距离。 I define a point structure which has a function for calculating distance to any other point.我定义了一个点结构,它有一个 function 用于计算到任何其他点的距离。

Testing this out on two points, it doesn't work.在两点上测试它,它不起作用。 I thought it might be the sqrt function not working but even the function "test" outputting a double doesn't work.我认为这可能是 sqrt function 不起作用,但即使是 function “测试”输出双精度也不起作用。

I am using VS Code and the output is... nothing.我正在使用 VS Code,而 output 是......什么都没有。

I'm sure I'm missing something simple...我确定我错过了一些简单的东西......

#include <iostream>
#include <cmath>

using namespace std;

struct point
{
        
        static int d;
        double *coords;

        point(){ //point class
            coords=new double[d];
            for (int i=0; i<d; i++){
                coords[i]=0;
            }
        }
        double dist(point &q){
                double squared=0;
                for (int i=0; i<d;i++){
                        squared+=(coords[i]-q.coords[i])*(coords[i]-q.coords[i]);
                }
                return sqrt(squared);
        }

        double test(){
                return 1.4;
        }
};

int point::d;

int main() {
        
    point p;
    int d=2;
    p.d=d;
    p.coords[0]=1;
    p.coords[1]=1;
    point q;
    q.d=d;
    q.coords[0]=0;
    q.coords[1]=2;
    
    std::cout << "\ndistance:" << p.dist(q)<<"\n\n";
    std::cout << "even this doesn't work!:" << p.test()<<"\n\n";

    return 0;
}

Couple of issues, some are crucial and some are conventional.几个问题,有些是至关重要的,有些是常规的。

  1. You better pass things as const and const your methods, so that you could use these methods from a const object.您最好将方法作为constconst传递,以便您可以从 const object 中使用这些方法。
  2. In order to change d , you should write point::d = 2 and not int d = 2为了改变d ,你应该写point::d = 2而不是int d = 2
  3. d are dimensions, and can not be negative, thus std::size_t or unsigned int d是维度,不能为负数,因此为std::size_tunsigned int
  4. If you allocate on the constructor, you should deallocate on the destructor.如果你在构造函数上分配,你应该在析构函数上释放。 If you have a destructor, you need a copy-constructor and copy-assignment operator.如果您有析构函数,则需要复制构造函数和复制赋值运算符。 If you don't know what these are, don't allocate in the constructor:-)如果您不知道这些是什么,请不要在构造函数中分配:-)
  5. Note that the point constructor is using point::d , and thus you should set point::d before creating any instance of p.请注意,点构造函数使用的是point::d ,因此您应该在创建 p 的任何实例之前设置point::d

I attached your code with my fixes applied我附上了您的代码并应用了我的修复程序

struct point
{
    static std::size_t d;
    double *coords;

    point() { //point class
        coords = new double[d];
        for (int i = 0; i < d; i++) {
            coords[i] = 0;
        }
    }

    double dist(const point &q) const {
        double squared = 0;
        for (int i = 0; i < d; i++) {
            squared += (coords[i] - q.coords[i])*(coords[i] - q.coords[i]);
        }
        return sqrt(squared);
    }

    double test() const {
        return 1.4;
    }

    point(const point& p) {
        coords = new double[d];
        for (std::size_t i = 0; i < d; ++i) {
            coords[i] = p.coords[i];
        }
    }

    point& operator=(const point& p) {
        if (this == &p) {
            return *this;
        }

        // coords are already allocated
        for (std::size_t i = 0; i < d; ++i) {
            coords[i] = p.coords[i];
        }
    }

    ~point() {
        delete[] coords;
    }
};

std::size_t point::d;


int main() {

    point::d = 2;
    point p;
    p.coords[0] = 1;
    p.coords[1] = 1;

    point q;
    q.coords[0] = 0;
    q.coords[1] = 2;

    std::cout << "\ndistance:" << p.dist(q) << "\n\n";
    std::cout << "even this doesn't work!:" << p.test() << "\n\n";

    return 0;
}

You code does nothing because the constructor of point will be called before you assign any value to d .您的代码什么也不做,因为在您将任何值分配给d之前将调用point的构造函数。 So by accident, d appears to have value of 0 (static variables are zero-initialized by default).因此,意外地, d的值似乎为 0(静态变量默认为零初始化)。

Here is one possibility to fix such code:这是修复此类代码的一种可能性:

#include <iostream>
#include <cmath>

using namespace std;

struct point
{
    constexpr static int d = 2;
    double coords[d];

    point(){ //point class
        for (int i=0; i<d; i++){
            coords[i]=0;
        }
    }
    double dist(point &q){
        double squared=0;
        for (int i=0; i<d;i++){
            squared+=(coords[i]-q.coords[i])*(coords[i]-q.coords[i]);
        }
        return sqrt(squared);
    }
};


int main(){

    point p;
    p.coords[0]=1;
    p.coords[1]=1;

    point q;
    q.coords[0]=0;
    q.coords[1]=2;

    std::cout << "\ndistance:" << p.dist(q)<<"\n\n";

    return 0;
}

Live Code实时代码

The problem is that you setting the member variable d after the constructor runs, so your double array always be an array of 0. You should provide d in your constructor parameters.问题是您在构造函数运行后设置成员变量 d ,因此您的双精度数组始终是 0 数组。您应该在构造函数参数中提供 d 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM