嗨,我在编译某些代码时遇到问题,我遇到了A依赖而B依赖A的情况。我提出了声明,但我不断遇到问题。

In file included from src/MemoWriteContext.h:7:0,
                  from src/MemoWriteContext.cpp:1:
src/MemoContext.h:29:20: error: field ‘memoWriteContext’ has incomplete type

MemoContext.h
#ifndef MEMOCONTEXT_H_
#define MEMOCONTEXT_H_

#include "sqlite/SqliteDb.h"
#include "Context.h"
#include "MemoWriteContext.h"

#include <string>
#include <memory>
#include <map>

namespace bbs
{
    class MemoWriteContext;

    class MemoContext : public Context
    {
    public:
        //'structors
        MemoContext(const std::map<std::string, std::shared_ptr<Context> > &_contexts,
                    sqlitecpp::SqliteDb &_sqliteDb);
        ~MemoContext();

    protected:
        //when called write the data back to the user
        void performAction(const std::string &data, std::shared_ptr<UserAgent> agent);

    private:
        MemoWriteContext memoWriteContext;
    }; //class memocontext
}

#endif  // MEMOCONTEXT_H_

MemoWriteContext.h
#ifndef MEMOWRITECONTEXT_H_
#define MEMOWRITECONTEXT_H_

#include "Context.h"
#include "sqlite/SqliteDb.h"
#include "sqlite/PreparedStmt.h"
#include "MemoContext.h"

#include <string>
#include <memory>
#include <map>

namespace bbs
{
    class MemoContext; //forward decl

    class MemoWriteContext : public Context
    {
    public:
        //'structors
        MemoWriteContext(const std::map<std::string, std::shared_ptr<Context> > &_contexts,
                        MemoContext &_memoContext, sqlitecpp::SqliteDb &_sqliteDb);
        ~MemoWriteContext();

    protected:
        //when called write the data back to the user
        virtual void performAction(const std::string &data, std::shared_ptr<UserAgent> agent);
        virtual void onReceiveUserAgent(std::shared_ptr<UserAgent> agent);
    private:
        MemoContext &memoContext; //parent;
        sqlitecpp::SqliteDb &sqliteDb;
        sqlitecpp::PreparedStmt writeMemoStmt;
        sqlitecpp::PreparedStmt findAgentIdStmt;
    };

    enum class MemoWriteState : char
    {
        USERNAME=0,
        MESSAGE,
        CONFIRM
    };

    class MemoWriteAgentData : public ContextAgentData
    {
    public:
        MemoWriteState state;
        int userId;
        std::string message;
    }; //class Memo Write Agent data

}

#endif  // MEMOWRITECONTEXT_H_

Full source here.

最佳答案

我认为您唯一的问题是MemoWriteContext.h具有#include "MemoContext.h"。上下文仅需要可以使用前向声明的引用。但是,如果您碰巧先包含MemoWriteContext.h,则它将在实际声明MemoContext.h之前引入class MemoWriteContext。然后,将使用class MemoWriteContext的前向声明而失败。您甚至可以在错误消息中看到订单。

只需删除该#include或至少反转MemoWriteContext.cpp中包含的顺序即可(因为每个.h包括另一个ojit_code都会有效地将它们反转)。

08-16 00:53