468,549 Members | 1,955 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,549 developers. It's quick & easy.

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 11222
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Matthew Del Buono | last post: by
5 posts views Thread by Ruben Campos | last post: by
3 posts views Thread by Serge Skorokhodov (216716244) | last post: by
4 posts views Thread by Amadeus W. M. | last post: by
8 posts views Thread by Jess | last post: by
2 posts views Thread by subramanian100in | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.