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

发布于 2024-10-08 19:18:33 字数 1907 浏览 1 评论 0原文

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

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

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

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-10-15 19:18:33

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

看来您可以通过

class bot_manager;

在一个或两个发布的头文件中使用前向声明而不是 #include "bot_manager.h" 来减少头依赖性。

I suspect a circular #include problem. Is it possible qanda.h indirectly includes 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文