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";
} 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
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"; }
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"; }
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
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!"
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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...
|
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
|
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
|
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>
| |
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
|
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
|
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
|
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>
|
by: subramanian100in |
last post by:
consider the following program
#include <iostream>
using namespace std;
class Rec
{
public:
Rec(int arg = 10) : val(arg) { }
|
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...
| |
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...
|
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. ...
|
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...
|
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...
|
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...
|
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...
| |
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...
|
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
| |