实体组件系统未定义符号 我是C++新手,我想用SFML来创建一个游戏,在这个游戏中我尝试使用一个实体组件系统,我可以用这个游戏和游戏在路上,但是当我编译时我会出错,我不知道如何修复它。

实体组件系统未定义符号 我是C++新手,我想用SFML来创建一个游戏,在这个游戏中我尝试使用一个实体组件系统,我可以用这个游戏和游戏在路上,但是当我编译时我会出错,我不知道如何修复它。,c++,entity-component-system,C++,Entity Component System,这是我的entity.hpp类: #ifndef Entity_hpp #define Entity_hpp #include <stdio.h> #include <iostream> #include <vector> #include <Component.hpp> using namespace std; class Entity { private: vector<unique_ptr<Component>

这是我的entity.hpp类:

#ifndef Entity_hpp
#define Entity_hpp

#include <stdio.h>
#include <iostream>
#include <vector>
#include <Component.hpp>

using namespace std;

class Entity {

private:
    vector<unique_ptr<Component>> _components;
public:
    void Start();
    void Update();
    void Draw();

    template <typename T, typename... TArgs> T& AddComponent(TArgs... args);
    template <typename T> T& GetComponent();
};

#endif /* Entity_hpp */
#如果NDEF实体(水电站)
#定义实体_水电站
#包括
#包括
#包括
#包括
使用名称空间std;
类实体{
私人:
向量分量;
公众:
void Start();
无效更新();
无效抽取();
模板T&AddComponent(目标…参数);
模板T&GetComponent();
};
#endif/*实体水电站*/
接下来是Entity.cpp类:

#include "Entity.hpp"

void Entity::Update() {
    for (auto& c : _components) {
        c->Update();
    }
}

void Entity::Draw() {
    for (auto& c : _components) {
        c->Draw();
    }
}

void Entity::Start() {
    for(auto& c : _components) {
        c->Start();
    }
}

template <typename T, typename... TArgs>
T& Entity::AddComponent(TArgs... args) {
    T* component(new T(forward<T>(args)...));

    component->entity = this;

    unique_ptr<T> ptr{component};
    _components.emplace_back(move(ptr));

    return *component;
}

template <typename T> T& Entity::GetComponent() {
    T* c;

    if(find(_components.begin(), _components.end(), c) != _components.end()) {
        return &c;
    }

    return nullptr;
}
#包括“Entity.hpp”
void实体::Update(){
对于(自动和控制:_组件){
c->Update();
}
}
void实体::Draw(){
对于(自动和控制:_组件){
c->Draw();
}
}
void实体::Start(){
对于(自动和控制:_组件){
c->Start();
}
}
模板
T&Entity::AddComponent(目标…参数){
T*分量(新T(正向(args)…);
组件->实体=此;
唯一的_ptrptr{component};
_组件。后置(移动(ptr));
返回*组件;
}
模板T&Entity::GetComponent(){
T*c;
如果(查找(_components.begin(),_components.end(),c)!=_components.end()){
返回&c;
}
返回空ptr;
}
我添加组件的游戏类:

#include "Game.hpp"

void Game::Start() {
    auto& entity(_system.AddEntity());
    auto& m(entity.AddComponent<Movement>());
}

void Game::Update() {

}
#包括“Game.hpp”
无效游戏::开始(){
自动实体(_system.AddEntity());
auto&m(entity.AddComponent());
}
无效游戏::更新(){
}
最后是我的组件及其父组件:

#ifndef MovementComponent_hpp
#define MovementComponent_hpp

#include <stdio.h>

#include <Component.hpp>

class Movement : Component {

public:
    Movement() {};

    void Start() override {};
    void Update() override {};
    void Draw() override {};
};

#endif /* MovementComponent_hpp */
\ifndef MovementComponent\u水电站
#定义MovementComponent_水电站
#包括
#包括
类移动:组件{
公众:
运动(){};
void Start()重写{};
void Update()重写{};
void Draw()重写{};
};
#endif/*移动组件\u水电站*/
两者在cpp文件中都没有实现

#ifndef Component_hpp
#define Component_hpp

#include <stdio.h>

using namespace std;

class Entity;

class Component {

public:
    Entity* entity{nullptr};
    virtual void Start();
    virtual void Update();
    virtual void Draw();
    virtual ~Component();
};


#endif /* Component_hpp */
\ifndef组件\u水电站
#定义水电站的组成部分
#包括
使用名称空间std;
类实体;
类组件{
公众:
实体*实体{nullptr};
虚拟void Start();
虚拟空更新();
虚空绘制();
虚拟组件();
};
#endif/*水电站部件*/
这就是我得到的错误:

Ld /Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Products/Debug/SFML.app/Contents/MacOS/SFML normal x86_64
    cd /Users/joeywelvaadt/Desktop/XCode/C++/SFML
    export MACOSX_DEPLOYMENT_TARGET=10.7
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -L/Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Products/Debug -L/usr/local/lib -F/Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Products/Debug -F/Library/Frameworks -filelist /Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Intermediates/SFML.build/Debug/SFML.build/Objects-normal/x86_64/SFML.LinkFileList -Xlinker -rpath -Xlinker @loader_path/../Frameworks -mmacosx-version-min=10.7 -Xlinker -object_path_lto -Xlinker /Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Intermediates/SFML.build/Debug/SFML.build/Objects-normal/x86_64/SFML_lto.o -stdlib=libc++ -fobjc-link-runtime -framework sfml-system -framework sfml-window -framework sfml-graphics -framework sfml-audio -framework sfml-network -Xlinker -dependency_info -Xlinker /Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Intermediates/SFML.build/Debug/SFML.build/Objects-normal/x86_64/SFML_dependency_info.dat -o /Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuuzvucxhodww/Build/Products/Debug/SFML.app/Contents/MacOS/SFML

Undefined symbols for architecture x86_64:
  "Movement& Entity::AddComponent<Movement>()", referenced from:
      Game::Start() in Game.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Ld/Users/joey-welvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuzvucxhodww/Build/Products/Debug/SFML.app/Contents/MacOS/SFML-normal x86\u 64
cd/Users/Joey Welvaadt/Desktop/XCode/C++/SFML
导出MACOSX_部署_目标=10.7
/Applications/Xcode.app/Contents/Developer/toolschains/Xcode default.xtoolschain/usr/bin/clang++-arch x86_64-isysroot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk-L/Users/joeyewelvaadt/Library/Xcode/derivedata/SFML eeksqjpgpgsdomeiuvucxhodww/Build/Products/Debug-L/usr/local/lib-F/Users/joeywelvaadt/Library/Developer/Xcode/DerivedData/SFML eeksqjpmpgsdomeiuzvucxhodww/Build/Products/Debug-F/Library/Frameworks-filelist/Users/joeyewavadt/Library/Developer/Xcode/SFML eeksqjpmpgsdomeiuzvucxhodww/Build/mediates/SFML.Build/Objects-normal/x86_64/SFML.LinkFileList-Xlinker-rpath-inker@loader\u path/./Frameworks-mmacosx version min=10.7-Xlinker-object\u path\u lto-Xlinker/Users/joey-welvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuzvucxhodww/Build/mediates/SFML.Build/Debug/SFML.Build/Objects normal/x86\u 64/SFML\lto-stdlib=libc++-fobjc-link运行时-framework-SFML系统-framework-SFML窗口-framework sfml graphics-framework sfml audio-framework sfml network-Xlinker-dependency_info-Xlinker/Users/Joey Welvaadt/Library/Developer/Xcode/DerivedData/sfml eeksqjpmpgsdomeiuuzvucxhodww/Build/mediates/sfml.Build/Objects-normal/x86_64/sfml_dependency_info.dat-o/Users/joey-welvaadt/Library/Developer/Xcode/DerivedData/SFML-eeksqjpmpgsdomeiuzvucxhodww/Build/Products/Debug/SFML.app/Contents/MacOS/SFML
架构x86_64的未定义符号:
“移动和实体::AddComponent()”,引用自:
Game::Game.o中的Start()
ld:找不到架构x86_64的符号
叮当声:错误:链接器命令失败,退出代码为1(使用-v查看调用)

模板声明和定义应该保留在同一个文件中(好吧,或多或少,您也可以通过在头的末尾包含实现文件来将它们分开,但为了清晰起见,让我简化一下)。您的
实体::AddComponent
成员函数并非如此,因此出现错误。
如果您想使用模板系统之类的功能,我建议您对该语言进行更深入的研究。

也就是说,请将成员模板函数的定义移动到
.hpp
并重试。

模板声明和定义应保留在同一个文件中(好吧,或多或少,您也可以通过在头的末尾包含实现文件来将它们分开,但为了清楚起见,让我简化一下). 您的
实体::AddComponent
成员函数并非如此,因此出现错误。
如果您想使用模板系统之类的功能,我建议您对该语言进行更深入的研究。

也就是说,请将成员模板函数的定义移动到
.hpp
并重试。

欢迎您。对于新的C++开发人员来说,这是一个非常常见的错误。不要忘记接受一个答案,如果它解决了你的问题。我不知道,不仅是C++新的,而且是新的。stackoverflow@DaniloWelvaadt那么,欢迎收看StackOverflow不客气。对于新的C++开发人员来说,这是一个非常常见的错误。不要忘记接受一个答案,如果它解决了你的问题。我不知道,不仅是C++新的,而且是新的。stackoverflow@DaniloWelvaadt那么,欢迎收看StackOverflow