简体   繁体   中英

C++ rookie: calculating distance between two points

I am trying to write some simple C++ code to output distance between points in d-dimensions. I define a point structure which has a function for calculating distance to any other point.

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.

I am using VS Code and the output is... nothing.

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.
  2. In order to change d , you should write point::d = 2 and not int d = 2
  3. d are dimensions, and can not be negative, thus std::size_t or unsigned 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.

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 . So by accident, d appears to have value of 0 (static variables are zero-initialized by default).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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