C++ 未声明标识符(但已声明?)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4528108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 15:39:18  来源:igfitidea点击:

C++ Undeclared Identifier (but it is declared?)

c++undeclared-identifier

提问by Joshua

I'm pretty sure I've included the qanda class, but when I try to declare a vector that contains it or a class of that type I get an error saying that qanda is undefined. Any idea what the problem might be?

我很确定我已经包含了 qanda 类,但是当我尝试声明一个包含它的向量或该类型的类时,我收到一条错误消息,指出 qanda 未定义。知道可能是什么问题吗?

bot_manager_item.h

bot_manager_item.h

#pragma once

#include "../bot_packet/bot_packet.h"
#include <vector>

class bot_manager_item;
#include "qanda.h"
#include "bot_manager.h"

class bot_manager_item
{
public:
    bot_manager_item(bot_manager* mngr, const char* name, const char* work_dir);
    ~bot_manager_item();

    bool startup();
    void cleanup();
    void on_push_event(bot_exchange_format f);
    bool disable;

private:
    void apply_changes();
    bot_manager *_mngr;

    std::string _name;
    std::string _work_dir;
    std::string _message;
    std::string _message_copy;
    std::vector<qanda> games;
    qanda test;

    char _config_full_path[2600];
};

qanda.h

强达

#ifndef Q_AND_A
#define Q_AND_A

#include "users.h"
#include "..\bot_packet\bot_packet.h"
#include "bot_manager.h"
#include <string>
#include <algorithm>
#include <map>
#include <vector>
#include <fstream>


class qanda
{
public:
    qanda(bot_manager * manager, std::string name, std::string directory);
    ~qanda(){};
    void room_message(std::string username, std::string user_message);
    void timer_tick();

private:
    // data members
    std::string question;
    std::string answer;
    std::string directory;
    std::string command_prefix;
    std::string name;

    Users users;
    std::map <std::string, std::string> questions_and_answers;

    int time_per_question; // seconds
    int time_between_questions; // seconds
    int timer; // milliseconds

    bool is_delayed;
    bool is_playing;

    bot_manager * manager;

    // functions
    void new_question();
    void send_message(std::string msg);
    void announce_question();
    void load_questions();

};

#endif

Solved: I ended up refactoring the code in such a way as to avoid the use of bot_manager within the qanda class.

已解决:我最终以这样一种方式重构了代码,以避免在 qanda 类中使用 bot_manager。

回答by aschepler

I suspect a circular #includeproblem. Is it possible qanda.h indirectly includes bot_manager_item.h?

我怀疑是循环#include问题。qanda.h 是否有可能间接包含 bot_manager_item.h?

It looks like you may be able to reduce header dependencies by using a forward declaration

看起来您可以通过使用前向声明来减少标头依赖性

class bot_manager;

instead of #include "bot_manager.h"in one or both of your posted header files.

而不是#include "bot_manager.h"在您发布的一个或两个头文件中。