'[' 标记前的预期不合格 ID
Expected unqualified-id before '[' token
我知道这个错误一般是语法错误,但我似乎找不到这段代码有什么问题。谁能帮我指出来?以下是我遇到的错误:
deli.cc:10:7: error: expected unqualified-id before ‘[’ token
int [] myCashierNums;
^
deli.cc:11:7: error: expected unqualified-id before ‘[’ token
int [] myOrderNums;
^
这是我在 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[]) {
}
这是 C++,不是 Java!
像这样声明数组:
int myCashierNums[1000];
int myOrderNums[1000];
请注意,C++ 中的数组在编译时必须有一个大小。在上面的例子中,它是 1000.
修改:
int myMaxOrders;
int* myCashierNums;
int* myOrderNums;
添加:
~SandwichBoard() {
if (myMaxOrders) {
delete [] myCashierNums;
delete [] myOrderNums;
}
}
我知道这个错误一般是语法错误,但我似乎找不到这段代码有什么问题。谁能帮我指出来?以下是我遇到的错误:
deli.cc:10:7: error: expected unqualified-id before ‘[’ token int [] myCashierNums; ^ deli.cc:11:7: error: expected unqualified-id before ‘[’ token int [] myOrderNums; ^
这是我在 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[]) {
}
这是 C++,不是 Java! 像这样声明数组:
int myCashierNums[1000];
int myOrderNums[1000];
请注意,C++ 中的数组在编译时必须有一个大小。在上面的例子中,它是 1000.
修改:
int myMaxOrders;
int* myCashierNums;
int* myOrderNums;
添加:
~SandwichBoard() {
if (myMaxOrders) {
delete [] myCashierNums;
delete [] myOrderNums;
}
}