2

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.

Joshua
  • 547
  • 1
  • 5
  • 13

1 Answers1

9

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.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • bot_manager includes bot_manager_item. Would that cause this? – Joshua Dec 24 '10 at 20:03
  • 1
    This allowed me to get past the building stage. Now I'm failing in linking, ugh. Bunch of stuff about how I have unresolved external symbols. Going to be reading up on what that might mean now. – Joshua Dec 24 '10 at 20:17
  • Nevermind. I cleaned the solution and tried again to discover that I'm still stuck on the building stage. I'm not sure how forward declarations work and when I used what you wrote it told me that bot_manager was undefined. – Joshua Dec 24 '10 at 20:50
  • @Josh is the error in the translation unit that implements bot_manager or client code that uses bot_manager? Keep in mind all a forward declaration does is tell the compiler that your class exist. If you have code that actually references methods or data members from this class, it must be fully defined. An #include "bot_manager.h" would be needed in the *compilation unit* in that case. – greatwolf Dec 25 '10 at 20:42