久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

對模板函數的未定義引用

undefined reference to template function(對模板函數的未定義引用)
本文介紹了對模板函數的未定義引用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有三個文件.main.cpp 的內容是

I have three files . The contents of main.cpp are

#include<iostream>
#include<QString>

#include "util.h"

int main()
{
    using Util::convert2QString;

    using namespace std;
    int n =22;
    QString tmp = convert2QString<int>(n);

    return 0;
}

util.h

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);
}

util.cpp

namespace Util
{
    template<class T>
        QString convert2QString(T type, int digits=0)
        {
            using std::string;

            string temp = (boost::format("%1%") % type).str();

            return QString::fromStdString(temp);
        }
}

當我嘗試使用以下命令編譯這些文件時,出現未定義的引用錯誤

When I try to compile these files with following command I get undefined reference error

vickey@tb:~/work/trash/template$ g++ main.cpp  util.cpp -lQtGui -lQtCore  -I. -I/usr/local/Trolltech/Qt-4.8.0/include/QtCore -I/usr/local/Trolltech/Qt-4.8.0/include/QtGui -I/usr/local/Trolltech/Qt-4.8.0/include
/tmp/cca9oU6Q.o: In function `main':
main.cpp:(.text+0x22): undefined reference to `QString Util::convert2QString<int>(int, int)'
collect2: ld returned 1 exit status

模板聲明或實現有問題嗎?為什么我會收到這些鏈接錯誤:?

Is there something wrong with the template declaration or implementation ? why M I getting these linking errors :?

推薦答案

非專用模板的實現必須對使用它的翻譯單元可見.

The implementation of a non-specialized template must be visible to a translation unit that uses it.

編譯器必須能夠看到實現,才能為代碼中的所有特化生成代碼.

The compiler must be able to see the implementation in order to generate code for all specializations in your code.

這可以通過兩種方式實現:

This can be achieved in two ways:

1) 將實現移到標題內.

1) Move the implementation inside the header.

2) 如果您想將其分開,請將其移動到包含在原始標題中的不同標題中:

2) If you want to keep it separate, move it into a different header which you include in your original header:

util.h

namespace Util
{
    template<class T>
    QString convert2QString(T type , int digits=0);
}
#include "util_impl.h"

util_impl.h

namespace Util
{
    template<class T>
        QString convert2QString(T type, int digits=0)
        {
            using std::string;

            string temp = (boost::format("%1") % type).str();

            return QString::fromStdString(temp);
        }
}

這篇關于對模板函數的未定義引用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 精品久久久精品 | 国产精品一区在线 | 精品久久久久久久人人人人传媒 | www.99精品| 成人免费在线小视频 | 亚洲第一成人av | 91成人小视频 | 欧美aaa一级片 | 99久久婷婷国产综合精品首页 | 日韩免费高清视频 | 国产精品乱码一区二三区小蝌蚪 | 亚洲精品一区二区 | 亚洲综合色网 | 日韩欧美一区在线 | 欧美一区二区三区在线看 | 91视频久久 | 亚洲视频免费在线观看 | 麻豆一区二区三区精品视频 | 日韩免费av | 亚洲在线 | 国产蜜臀97一区二区三区 | aaaaaaa片毛片免费观看 | av中文字幕在线 | 成年人黄色一级片 | 日韩久久网 | 欧美国产91 | 国产乱码一二三区精品 | 国产在线观看一区二区三区 | 91精品国产91久久综合桃花 | 一区二区三区av | 免费麻豆视频 | 久久久久成人精品免费播放动漫 | 免费不卡视频 | 亚洲精品中文字幕 | 国产精品久久视频 | 91影院在线观看 | 最新日韩在线视频 | 免费视频一区二区 | 亚洲区一 | 美女国内精品自产拍在线播放 | 国产探花在线精品一区二区 |