我一直在尝试为我一直在编写的游戏引擎创建一个组件系统。组件系统应该与Unity游戏引擎组件系统类似,例如:gameObject.GetComponent<ComponentType>();
我面临的问题是如何使用模板参数在向量容器中查找对象。下面是我认为可行的解决方案的代码示例,但是我收到两个编译器错误LNK2019和LNK1120。
//Gets the first occurence of the specified Component type
//If the specified component type can not be found in the component container a null pointer is returned
template<class ComponentType> Component* GameObject::GetComponent()
{
//Create temporary component
ComponentType temporaryComponent = ComponentType();
//For each component in the GameObjects component container
for (unsigned int componentIndex = 0; componentIndex < m_componentContainer.size(); componentIndex++)
{
//If the component at component index in the component container is the same type as temporary component
if (m_componentContainer[componentIndex]->GetType() == temporaryComponent.GetType())
{
//Return the component at component index in the component container
return m_componentContainer.data()->get() + componentIndex;
}
}
//The specified component type could not be found in the component container
return nullptr;
}
我知道这个函数可以写成:
Component* GetComponent(const unsigned char componentType);
但是,因为我正在编写一个Unity开发人员应该感觉很舒服的引擎,我希望尽可能准确地复制Unity的类结构。
为什么在尝试编译上面的代码时会收到链接器错误? 我目前没有链接到任何库/ SDK,也没有更改项目的任何解决方案设置。