下面的代码已在visual studio&solaris编译器上成功编译。但是在g++(suse linux)4.3.4中得到链接错误。请告诉我如何在Linux上修复此链接错误?注意:为了使代码简单明了,我在这里输入了代理代码。//————————————————————————————————————————————--#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//——————————————————————————————————————————————————————————--#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//—————————————————————————————————————————————————————————————--#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-g-ldl-lm-lnsl-m64-o-pthread-std=gnu++0x-bdynamic……生成“cmakefiles/templatetest.dir/b1.cpp.o-o../../../../../../templatetest-rdynamiccmakefiles/templatetest.dir/b1.cpp.o:函数中的LCD返回1退出状态**注2:已经在b1.cpp中定义了静态变量,如下所示,模板setpostype::addfunc setpostype::s_addfunc; (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 1.)一旦初始化成员变量,它就会工作:template <> setPosType::addFunc setPosType::s_AddFunc = 0;显然,没有初始值设定项,这一行是一个声明,而不是一个定义,但是,老实说,我不明白为什么。2)Clang告诉我,在它的命名空间之外定义静态成员是C++ 11的扩展。更好的使用namespace ServiceManagement { template <> setPosType::addFunc setPosType::s_AddFunc = 0;} (adsbygoogle = window.adsbygoogle || []).push({});
07-27 13:33