我想實現一個最近點的程序。下面是代碼:最近的點執行
#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;
}
但是,當我在命令行編譯它給出這樣的錯誤:
指針不在基類點的構件
這裏是完整列表:
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
]
我該如何解決這個問題?
對不起,但你確實知道衣櫥是什麼,對吧?有趣的是,你甚至可以在cpp文件中輸入錯字:)你可能想糾正這個問題。 除此之外,我認爲代替距離(a [i],a [j])它應該是[i] .distance(a [j]) – humbagumba 2010-08-24 08:23:20