简体   繁体   English

最近点实施

[英]Closest point implementation

I am trying to implement a closest point program. 我正在尝试实施最近的积分计划。 Here is the code: 这是代码:

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

float randfloat(){
    return 1.0*rand()/RAND_MAX;
}

class point
{
    public :
    float x,y;

    float distance(point& a){
        float dx=x-a.x;
        float dy=y-a.y;
        return  sqrt(dx*dx+dy*dy);
    }
};

int main(int argc,char* argv[]){
    int i,cnt=0;
    int n=atoi(argv[1]);
    float d=atof(argv[2]);
    point *a=new point[n];
    for (int i=0;i<n;i++)
    {
        a[i].x=randfloat();
        a[i].y=randfloat();
    }
    for (i=0;i<n;i++)
        for (int j=i+1;j<n;j++)
            if ((distance(a[i],a[j])<d)) cnt++;
    cout << cnt << "pairs within" << d << endl;

    return 0;
}

But when I compile it from the command line it gives an error like this: 但是,当我从命令行编译它时,会出现如下错误:

pointer is not member of base class point 指针不是基类点的成员

Here is the full list: 这是完整列表:

    C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) : error C
2039: 'iterator_category' : is not a member of 'point'
        closet_point.cpp(9) : see declaration of 'point'
        closet_point.cpp(37) : see reference to class template instantiation 'st
d::iterator_traits<_Iter>' being compiled
        with
        [
            _Iter=point
        ]
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) : error C
2146: syntax error : missing ';' before identifier 'iterator_category'
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) : error C
4430: missing type specifier - int assumed. Note: C++ does not support default-i
nt
C:\Program Files\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(373) : error C
2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base
 class of 'std::iterator_traits<_Iter>'
        with
        [
            _Iter=point
        ]

How can I fix this problem? 我该如何解决这个问题?

I believe that you are using the wrong distance() function. 我相信您使用的是错误的distance()函数。 I believe you meant 我相信你的意思

a[i].distance(a[j])

instead of 代替

distance(a[i],a[j])

You should call a member function in the form 您应该以以下形式调用成员函数

a[i].distance(a[j])

The problem you are seeing is because there is a built-in function called distance() , which expects two iterators. 您看到的问题是因为有一个名为distance()的内置函数,该函数需要两个迭代器。 Since point is not an iterator, the compiler spits these error which sounds totally irrelevant. 由于point不是迭代器,因此编译器会吐出这些错误,这听起来完全无关紧要。

(BTW, since .distance() does not modify the arguments, better declare it as float distance(const point& a) const .) (顺便说一句,由于.distance()不会修改参数,因此最好将其声明为float distance(const point& a) const 。)

Because you declared your distance as a member function of point , you should call it 因为您已将distance声明为point的成员函数,所以应将其称为

           if ((a[i].distance(a[j])<d)) cnt++;

Otherwise, it tries std::distance which causes the error messages. 否则,它将尝试std::distance导致错误消息。

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

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