C ++链接对象的文件(G ++) [英] C++ linking object's files (G++)

查看:128
本文介绍了C ++链接对象的文件(G ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  #includeclass.h

template< typename T>
void CIntegerType< T> :: Show(void){
cout<< m_Data< endl;
}

main.cpp
$ b

  #includeclass.h

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

UINT32。显示 ();

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< unsigned int> :: Show()'
collect2:ld返回1退出状态

解决方案

-in-the-header-file>为什么只能在头文件中实现模板?


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;
}

This commands return:

g++ -Wall -pedantic -c main.cpp

g++ -Wall -pedantic -c class.cpp

g++ -Wall -pedantic -o class.o main.o

main.o: In function `main': main.cpp:(.text+0x11): undefined reference to 'CIntegerType< unsigned int>::Show()' collect2: ld returned 1 exit status

解决方案

Try putting your template implementation in the header file.

See: Why can templates only be implemented in the header file?

这篇关于C ++链接对象的文件(G ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆