【问题标题】:Error: declaration does not declare错误:声明未声明
【发布时间】:2015-02-20 02:05:59
【问题描述】:

您好,我是 C++ 新手,来自 Java,所以当我尝试制作公共 ItemRegister 或其他任何内容时,我确实尝试构建一个小游戏,但它不起作用。我打算只拥有其中一个,并且需要公共访问。

游戏.h

#pragma once
#include "ItemRegister.h"


class Game{
    static ItemRegister register;

public:
    Game();
    ~Game();
private:
    Item test;
};

ItemRegister.h

#pragma once
#include <string>
#include <vector>
#include "Item.h"

using namespace std;
class ItemRegister{
    public:
    ItemRegister();
    ~ItemRegister();

    void registerItem(Item newItem);
    Item getItem(string name);
    Item getItemByID(int id);
    int getItemID(string name);

    int getRegisteredItemMax();
private:
    vector<Item> registeredItems;

};

ItemRegister 寄存器总是抛出错误。

【问题讨论】:

  • 它会抛出什么错误?
  • “ItemRegister 寄存器总是抛出错误。” 请问到底是哪个错误?请edit your question添加此重要信息!
  • 是ItemRegister 抛出错误,还是您从未初始化您的static ItemRegister?
  • 错误:声明未在 ItemRegister 上声明任何内容和错误:类成员的存储类无效
  • @David 好地方和猜测。让我们看看 OP 会澄清。

标签: c++ object header


【解决方案1】:

register 是 C++ 关键字,因此不能用作变量名。

class Game{
    static ItemRegister register;
                        ^^^^^^^^

您可以找到完整的 C++ 关键字列表here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-04
    相关资源
    最近更新 更多