0

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.

3
  • Did you include Item.h in Order.h? Here is not present. Commented Mar 1, 2016 at 16:09
  • Does Order.h include Item.h? Commented Mar 1, 2016 at 16:09
  • If you don't #include "Item.h" in Order.h, how would the compiler know what an Item is to generate code for handling it? You're going to have a similar problem in Item.cpp also. Commented Mar 1, 2016 at 16:24

1 Answer 1

3

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, I needed the #include "Item.h" before the Order class definition! It seems strange that Visual Studio would continue to highlight Item as a known class though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.