2012-05-17 19 views
55

私は3つのファイルを持っています。 main.cppをの内容は未定義テンプレート関数への参照

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

私は未定義の参照エラーを取得するには、次のコマンドを使用して、これらのファイルをコンパイルしようと

[email protected]:~/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テンプレートの宣言や実装に何か問題がありますか?なぜ私はこれらのリンクエラーを取得していますか?

答えて

88

特殊でないテンプレートの実装は、それを使用する翻訳単位で表示する必要があります。

コンパイラは、コード内のすべての特殊化のコードを生成するために、実装を認識できる必要があります。

これは、2つの方法で達成することができる。

1)ヘッダ内の実装を移動します。

2)あなたは、それは、分離あなたがあなたの元のヘッダーに含める異なるヘッダに移動維持したい場合:

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

多くの人がテンプレート実装のために '.tcc'拡張子を使用しますレズ。 –

19

あなたは2通りあります:

  1. convert2QStringをutil.hに実装します。

  2. 手動util.cppを内intconvert2QStringをインスタンス化し、util.h内でextern関数として特殊化を定義

util.h

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

    extern template <> QString convert2QString<int>(int type , int digits); 
} 

util.cppを

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

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

     return QString::fromStdString(temp); 
    } 

    template <> QString convert2QString<int>(int type , int digits); 
} 
関連する問題