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

對(duì)模板函數(shù)的未定義引用

undefined reference to template function(對(duì)模板函數(shù)的未定義引用)
本文介紹了對(duì)模板函數(shù)的未定義引用的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有三個(gè)文件.main.cpp 的內(nèi)容是

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

當(dāng)我嘗試使用以下命令編譯這些文件時(shí),出現(xiàn)未定義的引用錯(cuò)誤

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

模板聲明或?qū)崿F(xiàn)有問(wèn)題嗎?為什么我會(huì)收到這些鏈接錯(cuò)誤:?

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

推薦答案

非專(zhuān)用模板的實(shí)現(xiàn)必須對(duì)使用它的翻譯單元可見(jiàn).

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

編譯器必須能夠看到實(shí)現(xiàn),才能為代碼中的所有特化生成代碼.

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

這可以通過(guò)兩種方式實(shí)現(xiàn):

This can be achieved in two ways:

1) 將實(shí)現(xiàn)移到標(biāo)題內(nèi).

1) Move the implementation inside the header.

2) 如果您想將其分開(kāi),請(qǐng)將其移動(dòng)到包含在原始標(biāo)題中的不同標(biāo)題中:

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

這篇關(guān)于對(duì)模板函數(shù)的未定義引用的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫(xiě) for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線(xiàn)程)
Precise thread sleep needed. Max 1ms error(需要精確的線(xiàn)程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产精品美女久久久久av爽 | 四虎黄色片 | 一级黄色大片 | 亚洲第一免费视频 | 欧美日韩在线视频观看 | 国产激情视频在线 | 国产精品主播一区二区 | 一区二区三区精品 | 日韩国产精品视频 | 日韩精品久久久久久久 | www.av在线播放| av超碰在线 | 国产精品免费一区二区三区 | 日本va欧美va欧美va精品 | 亚洲区一区二 | 日本毛片在线观看 | 国产精品久久久久久久久久 | 亚洲一级黄色 | 亚洲精品久久久 | 超碰免费在线 | 日本在线网站 | 色综合天天 | 黄色三级av| 国产又猛又黄又爽 | 中文有码在线 | 亚洲国产免费 | 日韩一区二区三区在线 | 亚洲免费高清 | 午夜黄色大片 | 欧美精品三区 | 久久久精品一区二区三区 | 一区二区三区精品 | 久久国产精品99久久人人澡 | 人与拘一级a毛片 | 日本伊人网 | 成人动漫免费观看 | 精品www | 久久综合国产 | 黄色在线视频播放 | 亚洲成人a v| 好色影院 |