【问题标题】:C++ Undeclared Identifier (but it is declared?)C++ 未声明的标识符(但它已声明?)
【发布时间】:2010-12-24 19:50:44
【问题描述】:

我很确定我已经包含了 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。

【问题讨论】:

  • 你能贴出实际的错误信息吗?
  • 基本上是:Intellisense: qanda Undeclared Identifier.
  • 我正在努力解决其他错误。

标签: c++ undeclared-identifier


【解决方案1】:

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

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

class bot_manager;

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

【讨论】:

  • bot_manager 包括 bot_manager_item。这会导致这种情况吗?
  • 这让我能够通过构建阶段。现在我链接失败了,呃。一堆关于我如何有未解决的外部符号的东西。现在将阅读这可能意味着什么。
  • 没关系。我清理了解决方案并再次尝试发现我仍然停留在构建阶段。我不确定前向声明是如何工作的,当我使用你写的东西时,它告诉我 bot_manager 是未定义的。
  • @Josh 是实现 bot_manager 或使用 bot_manager 的客户端代码的翻译单元中的错误?请记住,前向声明所做的只是告诉编译器您的类存在。如果您的代码实际上引用了此类中的方法或数据成员,则必须对其进行完全定义。在这种情况下,编译单元中需要一个#include "bot_manager.h"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多