I have three files. The contents of 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); } }
When I try to compile these files with the following command, I get an undefined reference error link
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 declaring or implementing a template? why M am I getting these binding errors :?
c ++ undefined-reference boost qt
Vihaan Verma May 17 '12 at 8:32 2012-05-17 08:32
source share