template-based function in non-main file

S

szczeles

Hello everyone,
I'm trying to write some code in c++ using templates. When I place
simple template-based function in one file, for example:

//----main.cpp-----
#include <iostream>
using namespace std;

template <class T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << 1.5f << " + " << -0.4f << " = " << add(1.5f, -0.4f) << endl;
cout << 4 << " + " << 10 << " = " << add(4, 10) << endl;
}
//-----------------

everything works good, but if funcion add() is in another file, for
example in functions.cpp and its definition is in header file which I
include in main.cpp I can compile functions.cpp only, but `g++
main.cpp functions.cpp` results in error:

mario@laptok:~/cpptest$ g++ main.cpp functions.cpp
/tmp/ccW3MdFX.o: In function `main':
main.cpp:(.text+0x96): undefined reference to `float add<float>(float,
float)'
main.cpp:(.text+0x114): undefined reference to `int add<int>(int,
int)'

Is there any possibility to place template-based function in other
file?

Any help will be appreciated.
szczeles
 
E

Erik Wikström

Hello everyone,
I'm trying to write some code in c++ using templates. When I place
simple template-based function in one file, for example:

//----main.cpp-----
#include <iostream>
using namespace std;

template <class T>
T add(T a, T b) {
return a + b;
}

int main() {
cout << 1.5f << " + " << -0.4f << " = " << add(1.5f, -0.4f) << endl;
cout << 4 << " + " << 10 << " = " << add(4, 10) << endl;
}
//-----------------

everything works good, but if funcion add() is in another file, for
example in functions.cpp and its definition is in header file which I
include in main.cpp I can compile functions.cpp only, but `g++
main.cpp functions.cpp` results in error:

mario@laptok:~/cpptest$ g++ main.cpp functions.cpp
/tmp/ccW3MdFX.o: In function `main':
main.cpp:(.text+0x96): undefined reference to `float add<float>(float,
float)'
main.cpp:(.text+0x114): undefined reference to `int add<int>(int,
int)'

Is there any possibility to place template-based function in other
file?

http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
 
M

mictyd

Hello everyone,
I'm trying to write some code in c++ using templates. When I place
simple template-based function in one file, for example:

//----main.cpp-----
#include <iostream>
using namespace std;

template <class T>
T add(T a, T b) {
  return a + b;

}

int main() {
  cout << 1.5f << " + " << -0.4f << " = " << add(1.5f, -0.4f) << endl;
  cout << 4 << " + " << 10 << " = " << add(4, 10) << endl;}

//-----------------

everything works good, but if funcion add() is in another file, for
example in functions.cpp and its definition is in header file which I
include in main.cpp I can compile functions.cpp only, but `g++
main.cpp functions.cpp` results in error:

mario@laptok:~/cpptest$ g++ main.cpp functions.cpp
/tmp/ccW3MdFX.o: In function `main':
main.cpp:(.text+0x96): undefined reference to `float add<float>(float,
float)'
main.cpp:(.text+0x114): undefined reference to `int add<int>(int,
int)'

Is there any possibility to place template-based function in other
file?

Any help will be appreciated.
szczeles

You are so lame...
look here: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top