TransWikia.com

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

Stack Overflow Asked by theAndrewJeff on November 17, 2021

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.

One Answer

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.

Answered by Chris on November 17, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP