简体   繁体   English

从模板化函数返回结构指针

[英]Return a struct pointer from templated function

I keep receiving linker errors when I do this:当我这样做时,我不断收到链接器错误:

//function declaration
template<typename T>
T * EntityManager::GetComponent(EID _entity, CType _type)

//Main.cpp
Position * pos = GetComponent<Position>(eid, POSITION);

Error LNK2019 unresolved external symbol "public: struct Position * __thiscall EntityManager::GetComponent(unsigned int,enum CType)" (??$GetComponent@UPosition@@@EntityManager@@QAEPAUPosition@@IW4CType@@@Z) referenced in function _main错误 LNK2019 未解析的外部符号“public: struct Position * __thiscall EntityManager::GetComponent(unsigned int,enum CType)” (??$GetComponent@UPosition@@@EntityManager@@QAEPAUPosition@@IW4CType@@@Z) 在函数 _main 中引用

I believe the error resides in "struct Position * GetComponent(...)" I don't want it to return a "struct Position pointer" I want it to return a "Position pointer!"我相信错误存在于“struct Position * GetComponent(...)”我不希望它返回一个“struct Position pointer”我希望它返回一个“Position pointer!” I've tried various template prefaces such as "class" and "struct"我尝试过各种模板前言,例如“class”和“struct”

I would like for this to be possible as it is much more concise than我希望这成为可能,因为它比

Position * pos = static_cast<Position *>(GetComponent(eid, POSITION));

(which does work) (确实有效)

Thanks for any help!谢谢你的帮助!

EDIT: Here is the full source to prove that its not the function, but something to do with the template...编辑:这是证明它不是功能,而是与模板有关的完整来源...

//EntityManager.h
template<typename T>
T * GetComponent(EID _entity, CType _type);

//EntityManager.cpp
template<typename T>
T * EntityManager::GetComponent(EID _entity, CType _type)
{
    T * component = nullptr;
    int index = GetComponentIndex(_entity, _type);

    if (index >= 0)
        component = m_entities.find(_entity)->second[index];

    return component;
}

//Main.cpp
EntityManager EM;
Position * pos = EM.GetComponent<Position>(eid, POSITION);

struct Position inherits from struct Component struct Position 继承自 struct Component

As I said, the function works flawlessly if I remove the template and replace "T" with "Component" then static_cast the return value.正如我所说,如果我删除模板并将“T”替换为“组件”然后 static_cast 返回值,则该函数可以完美运行。 I want to avoid having to use the static cast我想避免使用静态转换

EDIT EDIT...编辑 编辑...

This compiles:这编译:

//EntityManager.h
class EntityManager
{
public:
    Component * GetComponent();
};

//EntityManager.cpp
Component * EntityManager::GetComponent()
{
 return new Position;
}

//Main.cpp
EntityManager EM;
Position * pos = static_cast<Position *>(EM.GetComponent());

This does not:这不会:

//EntityManager.h
class EntityManager
{
public:
    template<typename T>
    T * GetComponent();
};

//EntityManager.cpp
template<typename T>
T * EntityManager::GetComponent()
{
 return new T;
}

//Main.cpp
EntityManager EM;
Position * pos = EM.GetComponent<Position>();

WHY?为什么? All I'm asking is what format the template should be in.我要问的是模板应该采用什么格式。

(Yes, I tested this simplified example, please don't nitpick syntax) (是的,我测试了这个简化的例子,请不要挑剔语法)

You can't separate declaration and definition in classes which uses generic templates, as you do with non generic classes.您不能像处理非泛型类那样将使用泛型模板的类中的声明和定义分开。

Try to move all your EntityManager.cpp to EntityManager.h .尝试将所有EntityManager.cpp移动到EntityManager.h

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM