2012-03-06 32 views
0

我有凸包算法:语法错误

#include <cstdlib> 
#include <iostream> 

using namespace std; 
typedef int point; 
void convexhull(point x[],bool onedge){ 

     int N=sizeof(x)/sizeof(int); 
     int p=0; 
     bool used=new bool[N]; 
     for (int i=1;i<N;i++){ 
      if (x[i]<x[p]) 
      p=i; 


      } 
      int start=p; 
      do 
      { 

       int n=-1; 
       int dist=onedge?32756:0; 
       for (int i=0;i<N;i++){ 
        //dont go back to the same point you come from 
       if (i==p) continue; 
       if (used[i]) continue; 

        //if there is not such N yet,set it to x 
        if (n==-1) n=i; 
        int cross=(x[i]-x[p])*(x[n]-x[p]); 
        //d is distance from P to x 
        int d=(x[i]-x[p])*(x[i]-x[p]); 
        if (cross<0){ 
        n=i; 
        dist=d; 
          } 
          else if (cross==0){ 
           //in this case both N and X are4 in the 
           //same direction.if onedge is true 
           //pick the closest one,otherwidr pick farthest one 
           if (onedge && d<dist){ 

              dist=d; 
              n=i; 


              } 
              else if (!onedge && d>dist)} 
              dist=d; 
              n=i; 

              } 





           } 



       } 



       p=n; 
       used[p]=true; 
       } while(start!=p); 
} 
int main(int argc, char *argv[]) 
{ 
    system("PAUSE"); 
    return EXIT_SUCCESS; 
} 

但是,当我编译它,它让我看到象这样的错误:

26 G:\convex_hull.cpp invalid types `bool[int]' for array subscript 
48 G:\convex_hull.cpp expected primary-expression before '}' token 
67 G:\convex_hull.cpp expected `,' or `;' before '=' token 

请帮助我了解什么是错的。我不能使用整数下标与布尔数组?

回答

2

既然你宣布一个动态分配的数组,你需要一个指针:

bool* used=new bool[N]; 

你也有一个支架周围的其他方法:的

if (!onedge && d>dist) } 

代替

if (!onedge && d>dist) { 

顺便说一句,下次请采取适当缩进代码的时间。