C++语法错误
C++ Syntax Error
我收到此错误:
prog.cpp:1:5: error: expected unqualified-id before ‘[’ token
形成此代码:
int [] quick_srt(int array[], int low, int n){
请问这就是问题所在?
编辑
int [] quick_srt(int array[], int low, int n){
int lo = low;
int hi = n;
comp++;
if (lo >= n){ // lo is greater then or equal to n
return array;
}
int mid = array[(lo + hi) / 2]; //Using mid as pivot
comp++;
while (lo < hi){ //lo is less then hi
comp++;
while (lo < hi && array[lo] < mid){ //lo less than hi AND array[lo] less than mid
lo++;
comp++;
}
comp++;
while (lo < hi && array[hi] > mid) {//lo less than hi AND array[lo] greater than mid
hi--;
comp++;
}
comp++; //for if
comp++; //for else
if(array[lo] == array[hi]){
break; //for duplicate items
}
else if (lo < hi) { // less than
int T = array[lo];
array[lo] = array[hi];
array[hi] = T;
swaps++;
}
comp++;
}
comp++;
if (hi < lo) { //hi is less than lo
int T = hi;
hi = lo;
lo = T;
}
quick_srt(array, low, lo); //recrusie call
quick_srt(array, lo == low ? lo+1 : lo, n); //re-call, if lo = low, increment lo else pass lo and n
return array;
}
不能从函数返回数组。 参数列表中的int array[]
只是int *array
的语法糖,返回值没有类似物。 如果要返回指针,则需要显式执行此操作:
int *quick_srt(int array[], int low, int n);
或者,等效地:
int *quick_srt(int *array, int low, int n);
int []
仅对函数参数有效。我建议你这样做:
void quick_srt(std::vector<int>& array, int low)
你不需要n
参数。USe 'array.size() 代替。
另一种方法是使用void quick_srt(int * array, int low)
我建议使用矢量更容易
相关文章:
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- "错误 C0000:语法错误,令牌"<EOF>"处出现意外$end,并且不确定
- 我该怎么做才能摆脱使用命名空间时的语法错误?
- C++语法头文件错误
- 如何摆脱错误c2061;语法错误 cout?
- 语法错误:D3DX10Core 中的标识符'TEXTMETRICA'
- 错误 C2146:语法错误:标识符"A1"之前缺少","
- main.cpp(11):错误 C2059:语法错误:"字符串"
- 我想获取点的属性,它报告错误 C3867:"point::output_x":非标准语法;使用"&"创建指向成员的指针
- RedisGraph 语法错误在偏移量 8 靠近"创建"
- 如何修复此 FSM C++语法错误 C3867
- 函数错误 C2059:语法错误:'>'不起作用
- 为什么在main()之前断言会导致语法错误"error: expected ')' before numeric constant"?
- system() 返回错误语法错误:"("意外
- C++图形初始化错误(语法或丢失文件缺陷?
- 获取0:1(1):错误:语法错误,尝试编译着色器时意外$end
- Visual Studio错误:语法错误:标识符