【发布时间】:2013-05-28 20:33:16
【问题描述】:
class.h
#include <iostream>
#include <stdint.h>
using namespace std;
template <typename T>
class CIntegerType {
public:
void Show ( void );
private:
T m_Data;
};
class.cpp
#include "class.h"
template <typename T>
void CIntegerType<T> :: Show ( void ) {
cout << m_Data << endl;
}
main.cpp
#include "class.h"
int main ( void ) {
CIntegerType<uint32_t> UINT32;
UINT32 . Show ();
return 0;
}
此命令返回:
g++ -Wall -pedantic -c main.cpp
g++ -Wall -pedantic -c class.cpp
g++ -Wall -pedantic -o class.o main.o
main.o:在函数“main”中: main.cpp:(.text+0x11): 未定义的对 'CIntegerType::Show()' 的引用 collect2: ld 返回 1 个退出状态
【问题讨论】:
-
g++ -Wall -pedantic -o class.o main.o -- 看起来你缺少一个参数
-
你可能需要切换对象,我记错了,但我认为你必须在编译时首先指定你的主循环对象。
-
试试把模板函数放在表头。见:stackoverflow.com/questions/495021/…
标签: c++ g++ static-linking