0

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.

t_dog473
  • 27
  • 3
  • Possible duplicate of [What are forward declarations in C++?](https://stackoverflow.com/questions/4757565/what-are-forward-declarations-in-c) – Karl Knechtel Oct 27 '19 at 01:59
  • 2
    At this point in the code, the compiler cannot understand what `InventoryItem` means, because the definition of that class hasn't been seen yet. The C++ compiler expects to be able to interpret everything it sees in a single pass, top to bottom. A for ward declaration is needed (or define the classes the other way around); see the duplicate link I suggested. – Karl Knechtel Oct 27 '19 at 02:01
  • 2
    `InventoryItem*` -- The compiler works top-down. It has no idea what `InventoryItem` is. Also on a side note, you shouldn't have `using namespace std;` defined in any of your header files. Your usage of `string` instead of `std::string` indicates you're doing this. – PaulMcKenzie Oct 27 '19 at 02:01
  • Possible duplicate of [C++ Struct - Error 1 error C2143: syntax error : missing ';' before '\*'](https://stackoverflow.com/questions/19266713/c-struct-error-1-error-c2143-syntax-error-missing-before) – JaMiT Oct 27 '19 at 02:38

2 Answers2

2

You have not declared the class InventoryItem. Simply move:

class InventoryItem { }

above:

class InventorySystem { }
user4581301
  • 33,082
  • 7
  • 33
  • 54
Aaron Mann
  • 84
  • 9
2

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).

  • Since only references to `InventoryItem` are required by `InventorySystem`, the compiler will also accept a forward declaration. – user4581301 Oct 27 '19 at 02:19