c++ - 凸包算法中的语法错误

标签 c++ computational-geometry

我有凸包算法:

#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 

请帮助我了解哪里出了问题。我不能对 bool 数组使用整数下标吗?

最佳答案

因为你要声明一个动态分配的数组,所以你需要一个指针:

bool* used=new bool[N];

反过来你也有一个括号:

if (!onedge && d>dist) }

代替

if (!onedge && d>dist) {

顺便说一句,下次请花点时间正确缩进您的代码。

关于c++ - 凸包算法中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9580991/

相关文章:

c++ - 头文件,前向声明

algorithm - 来自边列表的多边形

c++ - 为什么我的凸包周长计算不起作用?

qt - QPolygonF/QLineF 相交

c++ - 我的 C++ 代码在 MS C++ 编译器上运行完美,但在 g++ 编译器上给我 NaN。为什么?

c++ - Qt Qml错误: Unable to assign [undefined] to QString in a subclassed QAbstractItemModel for Qml TreeView

c++ - 如何打包 C++ 项目中的所有库和依赖项?

c++ - 为什么 std::weak_ptr 没有 operator->?

algorithm - 在平面上找到 3 个最近的点

computational-geometry - 实现数据点的闭合 B 样条插值