473,583 Members | 4,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

link error if template function is defined in non-header file

Hi++,

I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.

For example...
*** Why am I getting a link error for template function f() in the code
below?

Thanks!
Suzanne

-----------------------------------
// File: Main.cpp

#include "Templates. h"

int main(int argc, char** argv) {
f<int>(5); // <--link error here
}
-----------------------------------
// File: Templates.h

#ifndef TEMPLATES_H
#define TEMPLATES_H

#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include iostream.h.
#endif // _STD_USING

#include <iostream>

template<class T>
void f(T n);

#endif // TEMPLATES_H
-----------------------------------
// File: Templates.cpp

#include "Templates. h"

template<class T>
void f(T n) {
std::cout << "I got an \"n\"!\n";
}

Jul 19 '05 #1
6 11524
"Suzanne" <su************ *@hotmail.com> wrote...
I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.

For example...
*** Why am I getting a link error for template function f() in the code
below?


Because the compiler doesn't get a chance to create the correct,
required, instantiation of the template if it doesn't see the body
of the function template when compiling the code that uses it.

Victor
Jul 19 '05 #2
coz, compiler can't see the definition of the function f.
so u shud include the definition of f in the header file, this is the
practice for all the template functions

Suzanne wrote:
Hi++,

I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.

For example...
*** Why am I getting a link error for template function f() in the code
below?

Thanks!
Suzanne

-----------------------------------
// File: Main.cpp

#include "Templates. h"

int main(int argc, char** argv) {
f<int>(5); // <--link error here
}
-----------------------------------
// File: Templates.h

#ifndef TEMPLATES_H
#define TEMPLATES_H

#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include iostream.h.
#endif // _STD_USING

#include <iostream>

template<class T>
void f(T n);

#endif // TEMPLATES_H
-----------------------------------
// File: Templates.cpp

#include "Templates. h"

template<class T>
void f(T n) {
std::cout << "I got an \"n\"!\n";
}


Jul 19 '05 #3
Hi Suzanne,

Template functions are only created if you "use" them. Nothing is happening
if you do not create an instance of a special type. Anyway, how should this
be possible?
This is exactly the case when your compiler creates the object file of your
cpp. So you have an object file with nothing inside.

Patrick

"Suzanne" <su************ *@hotmail.com> wrote in message
news:3e******** **@news.unc.edu ...
Hi++,

I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.

For example...
*** Why am I getting a link error for template function f() in the code
below?

Thanks!
Suzanne

-----------------------------------
// File: Main.cpp

#include "Templates. h"

int main(int argc, char** argv) {
f<int>(5); // <--link error here
}
-----------------------------------
// File: Templates.h

#ifndef TEMPLATES_H
#define TEMPLATES_H

#ifndef _STD_USING
#define _STD_USING // Must be #define'd in order to include iostream.h.
#endif // _STD_USING

#include <iostream>

template<class T>
void f(T n);

#endif // TEMPLATES_H
-----------------------------------
// File: Templates.cpp

#include "Templates. h"

template<class T>
void f(T n) {
std::cout << "I got an \"n\"!\n";
}

Jul 19 '05 #4
Suzanne <su************ *@hotmail.com> wrote in message news:<3e******* ***@news.unc.ed u>...
Hi++,

I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.


Theoretically, because you didn't include the keyword 'export' in the
declaration. That tells the compiler to defer template instantiation
to the linker.

In practice, most linkers can't do this yet and you need the definition
inlined in your header.

HTH,
--
Michiel Salters
Jul 19 '05 #5
Chandra Shekhar Kumar wrote:
coz, compiler can't see the definition of the function f.
so u shud include the definition of f in the header file, this is the
practice for all the template functions


I'm sorry if I seem like a real ass for saying this, but these
contractions are really getting up my nose for some reason. Is it
really that hard to type the word 'you' that you absolutely must reduce
it to 'u'? Same question for 'should' and 'shud'. Is your shift key
broken?

I don't know if this bugs anyone else. I've always thought of
programmers as being careful people who pay plenty of attention to case
and so on.

Ah hell, maybe you *did* learn English from an IRC channel. *shrug*

--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"

Jul 19 '05 #6
Corey Murtagh <em***@slingsho t.co.nz.no.uce> wrote in message news:<10******* ********@radsrv 1.tranzpeer.net >...
Chandra Shekhar Kumar wrote:
coz, compiler can't see the definition of the function f.
so u shud include the definition of f in the header file, this is the
practice for all the template functions


I'm sorry if I seem like a real ass for saying this, but these
contractions are really getting up my nose for some reason. Is it
really that hard to type the word 'you' that you absolutely must reduce
it to 'u'? Same question for 'should' and 'shud'. Is your shift key
broken?

I don't know if this bugs anyone else.


I'm in.
Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
28010
by: André Somers | last post by:
Hi, I'm having a problem that I have a hard time understanding. I am getting a "Fatal error: Call to a member function on a non-object" on a constructor of a class, that looks like this (simplified): class phpBB_Template { var $classname = "Template"; // Root template directory.
7
2667
by: Matthew Del Buono | last post by:
Don't try to solve the problem. I've found a way -- around or fixing it. I'm just curious as to whether this is Microsoft's problem in their compiler or if there's a standard saying this is to be true (not necessarily an internal compiler error, but still an error) This may just a bit OT, but I decided to post it here instead of Microsoft...
5
2624
by: Ruben Campos | last post by:
Some questions about this code: template <typename T> class MyTemplate; template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object); template <typename T> MyTemplate <T> operator- (const MyTemplate <T> & object1, const MyTemplate <T> & object2); template <typename T> class MyTemplate
3
1328
by: Serge Skorokhodov (216716244) | last post by:
Hi, I just seeking advice. Some background information first because I've run into issues that seems pretty obscure to me:( Quick search through KB yields next to nothing. Simplified samples work OK as well, the problems appear in rather bulky code:( I'm trying to implement some signal processing algorithm using
4
2494
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
9
13256
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include <hash_map>//from Standard template library //and some other headers
5
2357
by: chrisstankevitz | last post by:
Hi, Q1: Is there a way to make a template function that works only for specific types which produces a compiler error if used with an invalid type? Q2: If not, how do people deal with this issue? I believe A1 is no. I tried below with template function "f". For A2, functors work but have ugly syntax and runtime overhead. A
26
2971
by: Nospam | last post by:
I am trying to open a link in a new template window : <a onclick="windowopen('example.html','example');return false;" href="http://www.example.com" target="_blank"example link</a> such that clicking on the example link will open it in a new window in an already saved template/site/ called example.html
8
5306
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
2
1845
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7825
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8179
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8323
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7933
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5700
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3816
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2331
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 we have to send another system

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.