473,408 Members | 1,728 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,408 software developers and data experts.

Compiling template function

Hello I get linker error with files like these:

//function.h
template <typename T> void function(T*);

//function.cpp
template <typename T> void function(T* v)
{
...
}

// main.cpp
int main()
{
float v;
function(v);
}

g++ -o main main.o function.o <-- Linker error.
Jul 22 '05 #1
3 1274
> //function.h
template <typename T> void function(T*);

//function.cpp
template <typename T> void function(T* v)
{
...
}

// main.cpp
int main()
{
float v;
function(v);
}

Move the implementation of the template function to the header file so the
compiler can see it in main.cpp.

Martin
Jul 22 '05 #2

"Nafai" <na*******@yahoo.es> wrote in message
news:h7************************@telenews.teleline. es...
Hello I get linker error with files like these:

//function.h
template <typename T> void function(T*);

//function.cpp
template <typename T> void function(T* v)
{
...
}


All template code should go in header files.

john
Jul 22 '05 #3
Nafai posted:
Hello I get linker error with files like these:

//function.h
template <typename T> void function(T*);

//function.cpp
template <typename T> void function(T* v)
{
...
}

// main.cpp
int main()
{
float v;
function(v);
}

g++ -o main main.o function.o <-- Linker error.

Well, first thing that strikes me: Include "function.h" in
"main.cpp"!

#include "function.h"
Secondly, the entire template goes into the header file. Why? Because
it's dealt with entirely by the compiler, NOT the linker!

-JKop
Jul 22 '05 #4

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

Similar topics

0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
0
by: tyousaf | last post by:
Hi i am new to mysql and mysql++, i have installed mysql server, it is running fine. i also installed "mysql++-1.7.9gcc3.2-2.i386.rpm" (i have gcc 3.3) , first of all as the readme file says to do...
3
by: H. S. | last post by:
Hi, I am trying to compile these set of C++ files and trying out class inheritence and function pointers. Can anybody shed some light why my compiler is not compiling them and where I am going...
6
by: Josefo | last post by:
Hello all. I am a newbie following the C++ tutorial in : http://www.cplusplus.com/doc/tutorial/templates.html I am unable to succesfully compile any of the examples with templates of this...
2
by: renagade629 | last post by:
Can anybody help me understand what i'm doing wrong or what I'm missing? Is there anyother good and commendable C++ program I can use (free) from the internet like Dev C++? I'm having trouble doing...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...
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
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.