带有静态实例的未定义引用 [英] undefined reference with a static instance
问题描述
我目前正在将一些代码从Visual Studio移植到Mingw GCC. 当我尝试使用静态变量时,似乎出现了问题. 这段代码在Visual Studio中可以正常工作,所以我不确定这里可能是什么问题.我已经粘贴了代码外观的概述.如果这还不够的话,我可以投入更多.
I am currently porting some code from Visual Studio to Mingw GCC. It seems there is a problem when I attempt to use a static variable . This code works fine in Visual studio so I am not sure what the problem might be here. I have pasted an outline of what the code looks like. If this is insufficient I could put in more.
Header file: .h
namespace LG_Wrapper
{
template <LG_Thread Thread>
class EffectApplication : public ktApplication
{
public:
static EffectApplication<Thread>& GetInstance();
protected:
.....
.....
static boost::recursive_mutex mResource;
}
}
DECLARE_LEGACY_TYPES(EffectApplication);
Source File: .cpp
namespace LG_Wrapper
{
template<>
boost::recursive_mutex EffectApplication<LG_Thread_NAME>::mResource; //LG_Thread_NAME type discussed below
template <LG_Thread Thread>
EffectApplication<Thread>& EffectApplication<Thread>::GetInstance()
{
boost::recursive_mutex::scoped_lock mutex( mResource ); //Error with mResource
static EffectApplication<Thread> instance;
return instance;
}
....
}
现在我不确定LG_Thread_NAME
类型.
看完代码后,我注意到我在项目设置中定义了以下内容
Now I am not sure about the LG_Thread_NAME
type.
After going through the code I noticed that I define the following in the project settings
LG_THREAD_NAME=GAME
此定义在代码中的其他地方与该宏一起起作用
This definition comes into play with this macro somewhere else in the code
#define DECLARE_LEGACY_TYPES(...)
我在访问mResource
时遇到问题,如果我注释掉该语句,代码可以正常运行
I am having issue accessing mResource
The code builds fine if I comment out the statement
boost::recursive_mutex::scoped_lock mutex( mResource );
关于上述代码为什么会出现以下错误的任何建议?
Any suggestions on why I might be getting the following error with the above code ?
(.text$_ZN10KT_Wrapper17EffectApplicationILNS_9LT_ThreadE0EE11GetInstanceEv[_ZN10LT_Wrapper17EffectApplicationILNS_9LT_ThreadE0EE11GetInstanceEv]+0x11): undefined reference to `LT_Wrapper::EffectApplication<(LT_Wrapper::LT_Thread)0>::mResource'
更新: 我肯定变量mResource存在问题,因为在静态方法中.如果我这样做,它将建立良好的状态
Update: I am certain that there is something wrong with the variable mResource because in the static method. If I do this it builds fine
boost::recursive_mutex l;
boost::recursive_mutex::scoped_lock mutex( l );
问题重复: 我不确定这个问题如何与所提供的链接重复.在提供的所有链接中,操作尚未初始化其静态变量.在这里,我在类之外初始化了我的静态变量.
Duplicate Question Issue: I am not sure how this question is a duplicate of the links provided. In all the links provided the ops have not initialized their static variables. Here I have initialized my static variable outside the class.
推荐答案
初始化
template<>
boost::recursive_mutex EffectApplication<LG_Thread_NAME>::mResource;
仍然不正确,因为不是针对所有模板参数初始化mResource
,而是仅针对LG_Thread_NAME
的特殊化而初始化.因此,我想当您尝试使用其他模板参数访问它时,您会遇到错误.
still is not correct, since you initialize mResource
not for all template parameter, but only for specialization with LG_Thread_NAME
. So I guess when you try to access it with some another template parameter you've got an error.
正确的方法是这样
template<LG_Thread Thread>
boost::recursive_mutex EffectApplication<Thread>::mResource;
我也不确定我的答案,因为该错误表明问题出在LT_Wrapper
类,模板参数(LT_Wrapper::LT_Thread)0
对我来说很奇怪.
Also I'm not sure with my answer because the error says that the problem is with LT_Wrapper
class and template parameter (LT_Wrapper::LT_Thread)0
looks strange to me.
这篇关于带有静态实例的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!