简体   繁体   中英

Syntax error: missing ';' before '*' with Item class

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.

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 .

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