This question already has an answer here:
I keep getting undefined reference when i call the two functions from my template class "add" and "greater" in my main function.
So, i have: number.h
#ifndef NUMBER_H
#define NUMBER_H
template <class T>
class number {
public:
T x;
T y;
number (int a, int b){
x=a; y=b;}
int add (T&);
T greater ();
};
#endif
number.cpp
#include "number.h"
template <class T>
int number<T>::add (T& rezAdd){
rezAdd = x+y;
return 1;
}
template <class T>
T number<T>::greater (){
return x>y? x : y;
}
And my main file is: resolver.cpp
#include <stdio.h>
#include <stdlib.h>
#include "number.h"
int main (int argc, char **argv) {
int aux;
number<int> c(3,5);
c.add(aux);
printf ("number added [%d]\n", c.add(aux));
printf ("greater number: [%d]\n", c.greater());
return 0;
}
The errors that i keep getting are:
g++ -Wall -o tema1 resolver.cpp number.cpp
/tmp/ccX483J4.o: In function `main':
resolver.cpp:(.text+0x34): undefined reference to `number<int>::add(int&)'
resolver.cpp:(.text+0x47): undefined reference to `number<int>::add(int&)'
resolver.cpp:(.text+0x64): undefined reference to `number<int>::greater()'
collect2: ld returned 1 exit status
make: *** [all] Error 1
Thanks for the help in advance!
Your class is named wrong. Your class is named
cai
where all your functions belong to a class namednumber
: http://ideone.com/ZayX0cOne more thing.. you cannot have templates in the .cpp file. Template functions/defintions go in the header along with the class declaration. This is the reason for your undefined function error. Non-template functions go in the .cpp.
I prefer to have all of my functions in the
.cpp
file, regardless of whether they are template functions or regular functions. And there is a way to do that with some basic#ifndef
magic. Here's what you can do:main.cpp
myclass.hpp
myclass.cpp
Here's how the precompiler sees it. We have two
.cpp
files.myclass.hpp
MYCLASS
is undefined, and it ismyclass.cpp
MYCLASS_FUNCTIONS
MYCLASS_FUNCTIONS
is defined, it isMYCLASS_FUNCTIONS
is defined, it isn'tmyclass.hpp
MYCLASS
is undefined, and it ismyclass.cpp
myclass.hpp
againMYCLASS
is defined so do nothing inside, return tomyclass.cpp
MYCLASS_FUNCTIONS
is defined, it isMove the definitions of the
add
andgreater
function templates into yournumber.h
.Remember that
add
andgreater
aren't functions, they're function templates. To create actual functions, the compiler has to instantiate the template for specific types, such asint
, and it can only do that if it has access to the template's definition at the point where it discovers that an instance is needed.When you compile
number.cpp
, the compiler has access to the templates' definitions, but it doesn't see any code that requires a specific instance (such asnumber<int>
), so it doesn't generate instances.When you compile
resolver.cpp
, the compiler sees that it needs to instantiate those templates for theint
type, but it can't since it doesn't have their definitions. So it generates "external references", basically notes telling the linker to look for those functions in some other object file.The result is that the function templates don't get instantiated in either object file — in one because the compiler didn't know that it should, and in the other because it couldn't — so when the linker goes looking for them (to resolve those external references), it can't find them. That's why you get the error.
Moving the template function definitions into the header makes them visible to the compiler while it's compiling
main.cpp
, so it's able to instantiate those functions for theint
type. Function templates typically need to be defined in header files, rather than.cpp
files, for exactly this reason.