tags:

views:

43

answers:

3

i am trying to implement closest point program here is 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 command line it gives error like this pointer is not member of base class point here is 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
        ]

please help

+2  A: 

I believe that you are using the wrong distance() function. I believe you meant

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

instead of

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

tenfour
+1  A: 

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. Since point is not an iterator, the compiler spits these error which sounds totally irrelevant.

(BTW, since .distance() does not modify the arguments, better declare it as float distance(const point& a) const.)

KennyTM
+1  A: 

Because you declared your distance as a member function of point, you should call it

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

Otherwise, it tries std::distance which causes the error messages.

jpalecek