Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiling template function

Reply
Thread Tools

Compiling template function

 
 
Nafai
Guest
Posts: n/a
 
      04-23-2004
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.


 
Reply With Quote
 
 
 
 
Martin Gieseking
Guest
Posts: n/a
 
      04-23-2004
> //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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      04-23-2004

"Nafai" <(E-Mail Removed)> wrote in message
news:h76ic.4672616$(E-Mail Removed). 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


 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      04-23-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Error compiling template function within class template uvts_cvs@yahoo.com C++ 4 11-18-2004 05:30 PM
Error compiling template function within class template uvts_cvs@yahoo.com C++ 2 11-18-2004 04:33 PM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments