C++ C++;链接对象';s文件(G+;+;)

C++ C++;链接对象';s文件(G+;+;),c++,g++,static-linking,C++,G++,Static Linking,class.h #include <iostream> #include <stdint.h> using namespace std; template <typename T> class CIntegerType { public: void Show ( void ); private: T m_Data; }; #包括 #包括 使用名称空间std; 模板 类关联类型{ 公众: 无效显示(无效); 私人: T m_数据; }

class.h

#include <iostream>
#include <stdint.h>

using namespace std;

template <typename T>
class CIntegerType {
 public:
    void Show ( void );

 private:
    T m_Data;
};
#包括
#包括
使用名称空间std;
模板
类关联类型{
公众:
无效显示(无效);
私人:
T m_数据;
};
class.cpp

#include "class.h"

template <typename T>
void CIntegerType<T> :: Show ( void ) {
    cout << m_Data << endl;
}
#include "class.h"

int main ( void ) {
    CIntegerType<uint32_t> UINT32;

    UINT32 . Show ();

    return 0;
}
#包括“class.h”
模板
void CIntegerType::Show(void){

cout尝试
g++-Wall-pedantic-o main.o class.o
。您面临的问题与此问题相同:


链接器按函数出现的顺序搜索函数。由于您有一个模板函数,因此必须在实际代码之前将其在
main
中的使用反馈给链接器,以便在
类中实例化它


请参阅:

g++-Wall-pedantic-o class.o main.o--看起来您缺少一个参数您可能需要切换对象,我的内存不足,但我认为您必须在编译时始终首先指定主循环对象。请尝试将模板函数放在标题中。请参阅:谢谢。我尝试过了。输出:/usr/bin/ld:/usr/lib/debug/usr/lib/i386 linux gnu/crt1.o(.debug_info):重定位0具有无效的符号索引12…:在函数
\u start':(.text+0x18):未定义对
main“collect2:ld返回1个退出状态我尝试了变量“g++-Wall-pedantic-o main.o class.o”和“g++-Wall-pedantic-o class.o main.o”,但没有结果。