前向声明链接器错误

时间:2016-06-17 02:49:39

标签: c++ header forward-declaration

noobie 程序员在这里〜

我试图从我的演讲幻灯片中复制一些代码,但幻灯片不会显示头文件设置。两个类彼此使用(Agent和IBehaviour),所以在娱乐时我遇到了一个循环包含。我尝试使用前向声明解决方案(在IBehaviour中)来解决我的问题,但是现在我得到了一个未解决的外部错误......'

我想我可以看到我的问题〜我有一个函数在IBehaviour.h中指向一个不完整类型的指针,但是我不知道如何解决这个问题。

非常感谢任何帮助。以下代码已压缩为h文件以供阅读。

Agent.h

#pragma once
#include "IBehaviour.h"
#include <list>

class Agent
{
public:
    Agent();
    ~Agent();
    void Update();

protected:
    std::list<IBehaviour*> m_behaviours;
};

Agent::Agent()
{
}

Agent::~Agent()
{
}

void Agent::Update()
{
    for (auto iter = m_behaviours.begin(); iter != m_behaviours.end(); iter++)
        (*iter)->Update(this);
}

IBehaviour.h

class Agent;

class IBehaviour
{
public:
    IBehaviour();
    ~IBehaviour();

    virtual void Update(Agent* pAgent);
};

IBehaviour::IBehaviour()
{
}


IBehaviour::~IBehaviour()
{
}

void IBehaviour::Update(Agent * pAgent)
{
}

SeekB

#pragma once
#include "IBehaviour.h"

class SeekB : public IBehaviour
{
public:
    SeekB();
    ~SeekB();

    void Update(Agent *pAgent) override;
};

SeekB::SeekB()
{
}


SeekB::~SeekB()
{
}

void SeekB::Update(Agent * pAgent)
{
    // Apply seek force to pAgent
}

2 个答案:

答案 0 :(得分:0)

由于IBehaviour.h只处理指向类Agent的指针,并且没有使用该指针的实现,因此前向声明没问题。

当你开始使用那个指针时,你需要包含完整的类声明而不仅仅是前向声明。我假设您的SeekB课程使用 Agent课程,而不仅仅是指向它,因此您需要#include "Agent.h" SeekB.h文件。

除此之外,我看到你的代码中没有循环包含......

答案 1 :(得分:0)

主怜悯我没有主要功能。抱歉fam