-1

I'm encountering these errors:

1>...\inventory.h(9): error C2143: syntax error: missing ';' before '*'
1>...\inventory.h(9): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\inventory.h(9): error C2238: unexpected token(s) preceding ';'
1>...\inventory.h(15): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>...\inventory.h(15): error C2143: syntax error: missing ',' before '&'

When trying to compile the following code.

Inventory.h

#pragma once
#include "Includes.h"

class Inventory
{
private:
    int inv_cap;
    int inv_nrOfItems;
    Item **inv_itemArr;
    void expand();
    void initialize(const int from);
public:
    Inventory();
    virtual ~Inventory();
    void addItem(const Item &inv_item);
    void removeItem(int index);

};

Includes.h

#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <ctime>
#include "Functions.h"
#include "Character.h"
#include "Item.h"
#include "Inventory.h"

using namespace std;

Item.h

#pragma once
#include "Includes.h"

class Item
{
private:
    std::string i_name;
    int i_buyValue;
    int i_sellValue;


public:
    Item();
    virtual ~Item();

    inline void debugPrint() const
    {
        std::cout << i_name << std::endl;
    }


};

Based on the googling I've done, my problems seems to have to do with forward declaration, but I'm in the dark as how to fix it.

Anurag Dhadse
  • 1,722
  • 1
  • 13
  • 26
  • 3
    Circular include, I'd imagine. Not sure why you have an includes file like that. Only include what is actually needed. – ChrisMM Jul 22 '20 at 01:11
  • Thanks ChrisMM! I have other source files that use the other files in the Includes.h, but the compiler didn't start throwing errors until I added the Inventory class. Hope that helps! – theAndrewJeff Jul 22 '20 at 01:13
  • 1
    Try adding `class Item;` before the class declaration in `Inventory.h`. – H Krishnan Jul 22 '20 at 01:18
  • Also, you can remove `Includes.h` from `Inventory.h` and `Item.h`. – H Krishnan Jul 22 '20 at 01:42

1 Answers1

1

If you're coming from a higher-level language like C#, #include doesn't work the same as a using. #include essentially copies and pastes the entire file inline.

You can then see the problem of having Item.h include Includes.h which then includes Inventory.h, but then when Inventory.h tries to include Includes.h, it's already been included so it skips it (because of the #pragma once) and Item still isn't defined yet. In short, class Inventory ends up being defined above the class Item, but class Inventory expects the Item class to have already been defined.

It'd be best to delete the Includes.h file completely and have Inventory.h specifically include Item.h.

Chris
  • 325
  • 1
  • 3
  • 11
  • Are you saying that `Item.h` Not include `Includes.h` as well as `Inventory.h`? – Anurag Dhadse Jul 22 '20 at 05:23
  • 1
    `Item still isn't defined yet` That is the case, indeed, but `Inventory` does not need the full definition of `Item` at that point since it's only using pointers and references to it. So it would be enough to [forward declare](https://stackoverflow.com/questions/553682/when-can-i-use-a-forward-declaration) `class Item;` before it is used in `inventory.h`, as noted in another comment. – dxiv Jul 22 '20 at 05:44
  • Yep, this was exactly the problem! Attempting to forward declare class Item; only created more problems. Once I sorted out the various includes I was attempting to do, it had no problems at all. Thank you everyone for all of the helpful feedback! – theAndrewJeff Jul 22 '20 at 12:24