C++ '[' 标记前应为非限定 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28492647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expected unqualified-id before '[' token
提问by weskpga
I know this error is generally for syntax errors, but I can't seem to find anything wrong with this code. Can anyone help me point it out? Here are the errors I'm getting:
我知道这个错误通常是因为语法错误,但我似乎找不到这段代码有什么问题。谁能帮我指出来?以下是我收到的错误:
deli.cc:10:7: error: expected unqualified-id before ‘[' token int [] myCashierNums; ^ deli.cc:11:7: error: expected unqualified-id before ‘[' token int [] myOrderNums; ^
deli.cc:10:7: 错误: '[' token int [] myCashierNums 之前的预期不合格 ID;^ deli.cc:11:7: 错误:'[' token int [] myOrderNums 之前的预期未限定 ID;^
Here's the program I compiled using g++ on Ubuntu 14.04 64-bit.
这是我在 Ubuntu 14.04 64 位上使用 g++ 编译的程序。
#include <iostream>
#include <stdlib.h>
using namespace std;
class SandwichBoard {
//private:
int myMaxOrders;
int [] myCashierNums;
int [] myOrderNums;
//public:
SandwichBoard (int maxOrders) {
myMaxOrders = maxOrders;
myCashierNums = new int [maxOrders];
myOrderNums = new int [maxOrders];
// All values initialized to -1
for (int i = 0; i < maxOrders; i++){
myCashierNums[i] = -1;
myOrderNums[i] = -1;
}
}
// For debugging purposes
void printMyOrders() {
for (int i = 0; i < maxOrders; i++){
cout << "Cashier " << myCashierNums[i] << ", ";
cout << "Order " << myOrderNums[i] << endl;
}
}
int getMaxOrders () { return myMaxOrders; }
};
void cashier(void *in) {
}
void sandwich_maker(void *in) {
}
int main(int argc, char *argv[]) {
}
回答by CinCout
This is C++, not Java! Declare arrays like this:
这是C++,不是Java!像这样声明数组:
int myCashierNums[1000];
int myOrderNums[1000];
Please note that the arrays in C++ must have a size at compile time. In the above example, it is 1000.
请注意,C++ 中的数组在编译时必须具有大小。在上面的例子中,它是 1000。
回答by lokippc
modify:
调整:
int myMaxOrders;
int* myCashierNums;
int* myOrderNums;
add:
添加:
~SandwichBoard() {
if (myMaxOrders) {
delete [] myCashierNums;
delete [] myOrderNums;
}
}