By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 939 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Linker error with templatized function

P: n/a
Hi all,

This is the first time I've tried to create a templatized function, and I
can't get past a linker error. I create a new, unmanaged C++ application
and added a class named MyClass. In MyClass.h I added:

template <class T> void MyFunc(T);

In MyClass.cpp I added:

template <class T> void MyClass::MyFunc(T)
{
cout << T << endl;
}

In the main program, I added:

MyClass t;
t.MyFunc(25);

When I build the project, the compiler is happy, but the linker says:
Unresolved external symbol "public: void __thiscall
MyClass::MyFunc<int>(int)"

What have I missed?

(BTW, I just want to take a moment to thank the good folks who have been
answering my many questions in the NG recently. You guys are great!)
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I found the applicable section in my trusty "Ivor Horton's Beginning C++",
which points out that templates are best implemented entirely in the header
file; moreover, if the implementation is in a separate cpp file then an
"export" keyword needs to be inserted somewhere in the syntax, although it's
not clear where to apply that keyword. In any event, this code needs to be
reasonably portable, and Mr. Horton points out that "export" is not widely
implemented so... I'll put my implementation in the header file and be done
with it.

"Bob Altman" <rd*@nospam.com> wrote in message
news:OJ**************@TK2MSFTNGP10.phx.gbl...
Hi all,

This is the first time I've tried to create a templatized function, and I
can't get past a linker error. I create a new, unmanaged C++ application
and added a class named MyClass. In MyClass.h I added:

template <class T> void MyFunc(T);

In MyClass.cpp I added:

template <class T> void MyClass::MyFunc(T)
{
cout << T << endl;
}

In the main program, I added:

MyClass t;
t.MyFunc(25);

When I build the project, the compiler is happy, but the linker says:
Unresolved external symbol "public: void __thiscall
MyClass::MyFunc<int>(int)"

What have I missed?

(BTW, I just want to take a moment to thank the good folks who have been
answering my many questions in the NG recently. You guys are great!)

Nov 17 '05 #2

P: n/a
this is called "exporting". however, as far as I know, only one
compiler on the earth implemented standard's that facility (Comeau). As
of Visual Studio .NET 2003, compiler does this, but without export
keyword.

Implementing template in header is harmless, and clearer; the more
template arguments you have, the more complex code you deal with. The
simplest function declaration in a .cpp file would take longer.

Ismail

Nov 17 '05 #3

P: n/a
In article <OJ**************@TK2MSFTNGP10.phx.gbl>,
Bob Altman <rd*@nospam.com> wrote:
Hi all,

This is the first time I've tried to create a templatized function, and I
can't get past a linker error. I create a new, unmanaged C++ application
and added a class named MyClass. In MyClass.h I added:

template <class T> void MyFunc(T);

In MyClass.cpp I added:

template <class T> void MyClass::MyFunc(T)
{
cout << T << endl;
}

In the main program, I added:

MyClass t;
t.MyFunc(25);

When I build the project, the compiler is happy, but the linker says:
Unresolved external symbol "public: void __thiscall
MyClass::MyFunc<int>(int)"

What have I missed?


http://www.comeaucomputing.com/techt.../#whylinkerror
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.