Search code examples
c++arraysuser-defined-types

C++: Error with user defined type array


My errors seem to be derivative of this error:

error C2146: syntax error : missing ';' before identifier 'itemArray'

There is no missing semicolon but I also get this error on the same line:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

I don't understand why either of these errors occur.

Order.h

#ifndef ORDER_H
#define ORDER_H

const int MAX_ITEMS = 20;

class Order{

private:
    int items;
    Item itemArray[MAX_ITEMS]; //error seems to occur here
    int orderNum;
    int guestNum;
    double total;
    bool isOpen = true;

public:
    Order();
    ~Order();

    void AddItem();
    void DeleteItem();
    void ChangeItem();
    void CloseOrder();
    void DisplayOrderDetails();
    void SetGuestNum();
};

#endif ORDER_H

Order.cpp

#include "stdafx.h"
#include "Order.h"
#include "Item.h"
#include <iostream>

using namespace std;


...

Item.h

#ifndef ITEM_H
#define ITEM_H

#include "stdafx.h"
#include <string>
using namespace std;

class Item
{
private:
    double price = 0;
    string name = "";
    bool active = false;
    int itemNum = 0;

public:
    Item();
    ~Item();
    void CreateItem();
    void ChangeItemName();
    void ChangeItemPrice();
    void RemoveItem();

    bool GetActive();
    int GetItemNum();
    string GetName();
    double GetPrice();
};

#endif ITEM_H

Item.cpp

#include "stdafx.h"
#include "Item.h"
#include <iostream>
#include <string>


Item::Item()
{
    static int currentNum = 0;
    itemNum = ++currentNum;
}
...

What is the problem and how do I fix it? Any help is greatly appreciated, thanks.


Solution

  • It looks like Item is unknown before:

    Item itemArray[MAX_ITEMS]; //error seems to occur here
    

    it should work if you add: #include "Item.h" before Order class definition, or switch:

    #include "Order.h"
    #include "Item.h"
    

    to:

    #include "Item.h"
    #include "Order.h"