templates

Hello ,
I use Eclipse (Ubuntu) and here's the error in the following code.
any help 'd be appreciated :) ..

/home/mg/Documents/WorkSpace/DS/Debug/../main.cpp:55: undefined reference to `hello<int>::hello(int)'
/home/mg/Documents/WorkSpace/DS/Debug/../main.cpp:55: undefined reference to `hello<int>::~hello()'

1
2
3
4
5
6
7
8
9
10
11
12
13
 #ifndef HELLO_H_
#define HELLO_H_
template <typename H>
class hello {
public:
	hello(H);
	virtual ~hello();
private:
	H number;
};

#endif /* HELLO_H_ */


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "hello.h"
#include<iostream>
using namespace std;

template<typename H>
hello<H>::hello(H info) {
	cout << "Hello World";
	number = info;
	cout << number;
}
template <typename H>
hello<H>::~hello() {
	
}


1
2
3
4
5
6
7
8
#include"hello.h"
#include<iostream>

using namespace std;

int main () {
hello <int> hey(5);
}
Your function implementations also need to be in the header file:
http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
Thank you :)
Topic archived. No new replies allowed.