未解析的外部符号?

发布于 2024-09-28 15:01:09 字数 1241 浏览 2 评论 0原文

可能的重复:
什么是未定义引用/未解析的外部符号错误以及如何修复它?

我不擅长阅读 C++ 错误,但显然未解析的外部符号意味着我正在使用的函数未定义。我得到的错误是...

1>WorldState.obj : error LNK2001: unresolved external symbol "public: class Brutal::GameObject * __thiscall Brutal::GameObjectManager::createObject<class Player>(class Ogre::Vector3,class Ogre::Quaternion,class Brutal::PropertyList,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$createObject@VPlayer@@@GameObjectManager@Brutal@@QAEPAVGameObject@1@VVector3@Ogre@@VQuaternion@4@VPropertyList@1@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>C:\Users\Brett\Desktop\Factions Online\build\release\client\client.exe : fatal error LNK1120: 1 unresolved externals

这没有任何意义,因为 createObject 已定义,甚至当我右键单击它并单击“转到定义”时在 MSVC 中正确出现,

我调用它的代码是...

Brutal::GameObjectManager::getSingletonPtr()->createObject<Player>(Ogre::Vector3::ZERO, Ogre::Quaternion::IDENTITY);

所以我我错过了一些愚蠢的事情吗?

Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?

I am terrible at reading c++ errors, but obviously Unresolved External Symbol means the function I am using isn't defined. The error I am getting is...

1>WorldState.obj : error LNK2001: unresolved external symbol "public: class Brutal::GameObject * __thiscall Brutal::GameObjectManager::createObject<class Player>(class Ogre::Vector3,class Ogre::Quaternion,class Brutal::PropertyList,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??$createObject@VPlayer@@@GameObjectManager@Brutal@@QAEPAVGameObject@1@VVector3@Ogre@@VQuaternion@4@VPropertyList@1@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)
1>C:\Users\Brett\Desktop\Factions Online\build\release\client\client.exe : fatal error LNK1120: 1 unresolved externals

This doesn't make any sense as createObject is defined and even comes up appropriately in MSVC when I right click it and click "go to definition"

My code which calls it is...

Brutal::GameObjectManager::getSingletonPtr()->createObject<Player>(Ogre::Vector3::ZERO, Ogre::Quaternion::IDENTITY);

So am I missing something silly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十六岁半 2024-10-05 15:01:09

由于您使用的是模板函数,因此在调用它时它的定义必须是可见的。因此,因为这是一个成员函数,所以它必须在声明它的标头中实现。

Since you are using a template function, it's definition has to be visible when you call it. Therefore, because this is a member function, it has to be implemented in the header where you declared it.

你怎么敢 2024-10-05 15:01:09

鉴于它是一个模板,您是否将实现放在与标头不同的文件中? (您可以对您决定声明拥有模板实现的特定类执行此操作,称为实例化)。

我将扩展我之前的文章,因为我可以在这里比在评论中更容易地提出编码建议。

您有一个名为 createObject 的模板函数。您可能会考虑“重构”它以使用类,因为更容易将其挑出来。

这是示例代码,因此请填写详细信息以使其适用于您的确切示例。

class GamePlayerManager
{
public:
   // all the other stuff
   template< typename T > createObject(/*params */)
   {
     // implement inline
       res = GameObjectFactory<T>::create(/* whatever parameters */);
   }
};

我现在已将对象创建单独放入名为 GameObjectFactory 的自己的类中。在 Player 可见的某个时刻,您可以实例化此模板,同时隐藏其 create() 方法。

template class GameObjectFactory<Player>;

这足以让编译器知道它需要在实现中查找此类函数的实现(它将其提升为“真正的”类,以便您可以将实现放入其 .cpp 文件中)。请注意,这是一个实例化,而不是专门化,因此您的模板 GameObjectFactory 仍然可以实现您想要内联的任何函数,并且您不必为您的类重新定义它们。这是“重用”通用代码的好方法,同时允许您专注于一些细节。

Given that it is a template, have you put the implementation in a different file from the header? (You can do this for specific classes you decide to declare you have a template implementation for, known as instantiation).

I am going to extend my previous post as I can put in coding suggestions here more easily than in a comment.

You have a template function called createObject. You might consider "refactoring" this to use a class because it is easier to single this out.

This is sample code so fill in the detail to make it work for your exact example.

class GamePlayerManager
{
public:
   // all the other stuff
   template< typename T > createObject(/*params */)
   {
     // implement inline
       res = GameObjectFactory<T>::create(/* whatever parameters */);
   }
};

I have now singled out object creation into its own class called GameObjectFactory. At some point where Player is visible you can instantiate this template whilst hiding its create() method.

template class GameObjectFactory<Player>;

This is good enough for the compiler to know it needs to look in the implementation for the implementation of this class's functions (it promotes it into a "real" class so you can put the implementation into its .cpp file). Note that this is an instantiation, not a specialisation, so your template GameObjectFactory could still implement whatever functions you want to inline and you would not have to redefine them for your class. This is a good way to "reuse" the common code whilst allowing you to specialise on some of the detail.

甜点 2024-10-05 15:01:09

您似乎没有链接到关联的库。您已包含标头,因此编译器不会抱怨,但链接器不知道您在说什么。

It looks like you're not linking to the associated library. You've got the header included, so the compiler isn't complaining, but the linker doesn't know what you're talking about.

人心善变 2024-10-05 15:01:09

createObject 是否采用默认参数还是重载?

如果是这样,您是否定义了该函数的所有版本?

在错误消息中,它显示的函数参数比调用中的参数多。

如果函数是在其他库中定义的,则必须通过将该库作为项目设置中链接器的输入来将其与您的应用程序链接。

Is createObject taking default arguments or is it overloaded ?

If so, are you defining all versions of that function?

In the error message, its showing more parameters for function than in the call.

And If functions is defined in some other library, you have to link it with your application by giving that library as input to Linker in Project settings.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文