首页 > 解决方案 > 为什么我收到语法错误:缺少';' 前 '*'

问题描述

在类声明中,我在两行上收到此错误。当我尝试声明一段成员数据时,我得到了它,这是一个指向具有全局常量 int 数组大小的对象的指针数组。我第二次收到错误是当我声明一个成员函数时,它返回一个指向与数组相同类型的对象的指针。这两个都是类的私有成员。

我知道我没有错过数组的类声明和函数返回值的结束分号。我尝试将语法中的星号移动为靠近变量名、靠近变量类型以及介于两者之间。

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_;
};

我收到很多错误消息,但似乎都可以追溯到这两个。我无法编译我的代码,也不知道为什么。如果我能提供更多相关信息,请告诉我。谢谢。

标签: c++

解决方案


你还没有声明类InventoryItem。简单移动:

class InventoryItem { }

多于:

class InventorySystem { }

推荐阅读