Undefined reference to 'function' error when linking

I realize that this is a error that has probably been encountered before, but after searching the internet for hours and trying every solution I have seen suggested I still have not found one that works. I tried making a "Hello World!" type program of a similar format to see if I could identify the problem or solve it but I still get the Undefined reference to 'function' error. The following is the code I'm trying to compile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//hello.hpp file:
#include <stdio.h>

template <class Data>
class hello{
public:
  hello(Data d);
  void print();
private:
  Data d;
};


//hello.cpp file:
#include "hello.hpp"

template <class Data>
hello<Data>::hello(Data data){
  this->data=data;
  printf("Hello world!\n");
}

template <class Data>
void hello<Data>::print(){
  printf("I work properly");
}


// and the main file with main function hellomain.cpp:
#include "hello.hpp"

int main(int argc, char* argv[]){
  hello<int> h=hello<int>(1);

  h.print();
}

I try to compile the files with the following commands in terminal:
1
2
3
g++ -Wall -c hello.cpp
g++ -Wall -c hellomain.cpp
g++ -Wall -o hello hello.o hellomain.o

I also tried the following:
 
g++ -Wall -o hello hello.cpp hellomain.cpp

Yet I always get these errors when I try to compile the executable:
1
2
hellomain.cpp:(.text+0x19): undefined reference to `hello<int>::hello(int)'
hellomain.cpp:(.text+0x25): undefined reference to `hello<int>::print()'

I was hoping someone could help me figure out why I keep on getting this error.
Thank you very much that helped solve my problem
Topic archived. No new replies allowed.