C++:用户定义类型数组错误

标签 c++ arrays user-defined-types

我的错误似乎是这个错误的衍生:

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

没有遗漏分号,但我也在同一行收到此错误:

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

我不明白为什么会出现这些错误。

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

项目.cpp

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


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

问题是什么,我该如何解决? 非常感谢任何帮助,谢谢。

最佳答案

看起来 Item 以前是未知的:

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

如果您在 Order 类定义之前添加:#include "Item.h" 或切换:

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

到:

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

关于C++:用户定义类型数组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35727751/

相关文章:

c++ - 为什么我在赋值后得到不同的指针值?

javascript - javascript/jquery 数组迭代有什么问题

c - 执行 typedef 时初始化结构数组

asp.net - ASMX Web 服务 - 返回具有属性的用户定义的类

c++ - 在 Eclipse 中向 'link' 命令添加引号

c++ - 使用 OpenAL 和 FFMPEG 流式传输音频

Python:使用正则表达式创建不重复条目的数组

c# - 无法将用户定义的数据类型参数添加到我的查询中

sql-server - 内存中用户定义表,不在内存中?

c++ - 允许转义 `?` 的理由是什么?