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 템플릿 선언 또는 구현에 문제가 있습니까? 왜 M이 링크 오류가 발생합니까?
많은 사람들이 템플릿 구현을 위해'.tcc' 확장자를 사용합니다 레. –