473,467 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Linker error with templatized function

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
3 1073
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: aurgathor | last post by:
Howdy, I got the templatized class Matrix working in the way it's written in the faq , and it works fine as: Matrix<sqr_T> display(80,25); However, I'd like to have this variable in the...
3
by: Chucker | last post by:
Hi Folks, I got a Wrapper Dll around a native C++ static library. In .NET 1.1 this worked fine. When moving to .NET 2.0 I get a couple of unresolved externals / linker errors: Error 16 error...
0
by: VivekR | last post by:
I have a MFC application developed using VC++ 5. Recently I ported that to VC++ 7.1 and now I am trying to compile the MFC application with /CLR under VC++ 7.1. And I get linker errors referring to...
4
by: PyongHopscotch | last post by:
Hi All, So I'm getting the generic linker error (unresolved external error) in VC 7.0 when I define a templatized function. Here's some code examples: template <class T> class...
1
by: developer | last post by:
Hi All I have made a .NET project. the files included are borland c++ files that i am migrate to VC++ .NET I am using Microsoft Visual C++ .NET 2003. the compilation goes through properly,...
3
by: prakash.mirji | last post by:
Hello, I am getting below mention linker error when I tried to link my class test.C I use below command to compile test.C /usr/bin/g++ -g -fpic -fvisibility=default -D_POSIX_SOURCE...
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
1
by: Deepath G | last post by:
This is deepath.. I am getting some linker error when i am trying to connect Websphere MQ using Borland C++ Builder 2006 using imqi.hpp on windows. Error Message ----------------------- ...
3
by: Rahul | last post by:
Hi Everyone, I have the following polymorphic classes, class Shape { public : virtual void draw() { } virtual void sample();
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.