我试图做这样的事情:链接错误,当我使用模板和静态变量
class A
{
static pthread_mutex_t m;
public:
template<typename T>
static void TestFunc(T t) {
pthread_mutex_lock(&m);
}
static void Test()
{
TestFunc(13);
}
};
但收到链接错误:
/tmp/cc1HN0fI.o: In function
void A::TestFunc<int>(int)': TReaderThread.cpp:(.text._ZN1A8TestFuncIiEEvT_[_ZN1A8TestFuncIiEEvT_]+0xc): undefined reference to
A::m' collect2: error: ld returned 1 exit status make: *** [all] Error 1
看来,这个错误发生,只有当TestFunc是模板功能。这是代码的小和平,我需要 TestFunc是模板,我的互斥体是静态变量。是否有可能在模板处理中解决这个问题? P.S.真的,我需要做 - 实施示踪剂singlton(同步输出描述符)。
您定义了'm',对吗? –