gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:39:58 25 4
gpt4 key购买 nike

我有凸包算法:

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com