波纹代码在Visual Studio &上成功编译Solaris编译器。但在g ++(SUSE Linux)4.3.4中获取链接错误。请让我知道如何解决这个链接错误在Linux? 注意:为了使代码更简单,更清洁,我在此输入了代理代码。C++模板类 - >静态函数 - >静态函数指针的链接错误
// ---------------- a1.h -----------
#ifndef __a1_h__
#define __a1_h__
#include <iostream>
#include <memory>
namespace ServiceManagement
{
template <typename T>
class ClassA1
{
public:
ClassA1() {}
typedef std::auto_ptr<T>(*addFunc)(int, int);
static void setAddFunc(addFunc f)
{
s_AddFunc = f;
}
private:
static addFunc s_AddFunc;
};
}
#endif
// - -------------- b1.h -----------
#ifndef __b11_h__
#define __b11_h__
#include "a1.h"
typedef ServiceManagement::ClassA1<int> setPosType;
namespace ServiceManagement
{
class ClassB1
{
public:
ClassB1() {}
static void Register();
private:
std::auto_ptr<setPosType> m_ptrMsg;
};
}
#endif
// ----------- ----- b1.cpp -----------
#include "b1.h"
#include <iostream>
using namespace std;
//setPosType::addFunc setPosType::s_AddFunc;
template <> setPosType::addFunc setPosType::s_AddFunc;
namespace AA
{
namespace v2
{
std::auto_ptr<int> message2_(int a, int b)
{
int c = a + b;
std::auto_ptr<int> p1(new int);
*p1.get() = c;
return p1;
}
}
}
namespace ServiceManagement
{
void ClassB1::Register()
{
setPosType::addFunc f1 = ::AA::v2::message2_;
setPosType::setAddFunc(f1);
}
}
int main()
{
int i = 0;
cin >> i;
return 0;
}
链接错误: /usr/bin/C++ -pthread -g -w -W -Wall -Wno-long-long -g -ldl -lm -lnsl -m64 -o -pthread -std = gnu ++ 0x -Bdynamic
.. build“CMakeFiles/templateTest.dir/b1.cpp.o -o ../../../../../..//templateTest -rdynamic CMakeFiles/templateTest。 DIR/b1.cpp.o:在功能**ServiceManagement::ClassA1<int>::setAddFunc(std::auto_ptr<int> (*)(int, int))'**: /templateTest/**a1.h:18: undefined reference to
ServiceManagement :: ClassA1 :: s_AddFunc” collect2:LD返回1个退出状态**
注2: 已经静态变量在b1.cpp被定义喜欢 模板<> setPosType :: addFunc setPosType :: s_AddFunc;
你的问题的格式化已经变得很糟糕了,我无法制作错误日志的头部或尾部。你可以编辑它,并使用反引号('''为嵌入代码,'
'为(html转义)输出? – Quentin