473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 11515
"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.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.


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***@slingshot.co.nz.no.uce> wrote in message news:<10***************@radsrv1.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
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...
7
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...
5
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>...
3
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...
4
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
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...
5
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...
26
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...
8
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
by: subramanian100in | last post by:
consider the following program #include <iostream> using namespace std; class Rec { public: Rec(int arg = 10) : val(arg) { }
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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
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...

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.