波紋代碼在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