Expected unqualified-id before '[' token

2019-03-26 23:11发布

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; ^

Here's the program I compiled using g++ on Ubuntu 14.04 64-bit.

#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++ g++
2条回答
Ridiculous、
2楼-- · 2019-03-26 23:44

This is C++, not Java! Declare arrays like this:

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.

查看更多
劳资没心,怎么记你
3楼-- · 2019-03-26 23:55

modify:

int myMaxOrders;
int* myCashierNums;
int* myOrderNums;

add:

~SandwichBoard() {
    if (myMaxOrders) {
       delete [] myCashierNums;
       delete [] myOrderNums;
    }
}
查看更多
登录 后发表回答