简体   繁体   中英

Why am I getting Syntax error: missing ';' before '*'

In a class declaration I'm getting this error on two lines. I get it when I try to declare a piece of member data, an array of pointers to objects with array size of global constant int. The second time I get the error is when I declare a member function which returns a pointer to the same type of object with the array. Both of these are private members of the class.

I know that I'm not missing the ending semicolon for the class declaration for the array and the function return value. I've tried moving the asterisk in my syntax as either close to the variable name, close to the variable type, and in between the two.

class InventorySystem {
public:
   InventorySystem();
   InventorySystem(int store_id, string store_name);
   ~InventorySystem();
   void set_store_name(string store_name);
   void set_store_id(int store_id);
   void set_item_count(int item_count);
   string get_store_name(void) const;
   int get_store_id(void) const;
   int get_item_count(void) const;
   void BuildInventory(void);
   void ShowInventory(void) const;
   void UpdateInventory(void);
   void Terminate(void) const;
private:
   string store_name_;
   int store_id_;
   InventoryItem *p_item_list_[g_kMaxArray];            // THIS LINE
   int item_count_;
   InventoryItem* FindInventoryItem(int item_id) const; // THIS LINE
};

class InventoryItem {
public:
   InventoryItem();
   InventoryItem(bool restocking);
   virtual ~InventoryItem();
   void set_restocking(bool restocking);
   bool get_restocking(void) const;
   int get_item_id(void) const;
   void Display(void) const;
protected:
   int item_id_;
   bool restocking_;
};

I'm getting a lot of error messages, but all of them seem to be traced back to these two. I can't get my code to compile and I don't know why. Please let me know if I can provide more relevant information. Thanks.

You have not declared the class InventoryItem . Simply move:

class InventoryItem { }

above:

class InventorySystem { }

This is a symptom (though I recon it could be something else) that InventoryItem is undeclared. This means that either a) you need to include the header that declares this class above the declaration of InventorySystem or b) InventoryItem is actually declared after InventorySystem in your actual code (that is, you haven't changed it by copying it here). If this is the case, you need rearrange the declarations.

Note that you also have the option of a forward declaration (thanks user4581301).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM