G
Giannis Papadopoulos
I'm new to C++. I want to implement a simple example with a template class.
I have written the following:
A.hpp
-----
#ifndef A_H
#define A_H
template<class T> class A {
private:
T val;
public:
A(T v);
~A(void);
T getA(void) const;
};
#endif
A.cpp
-----
#include "A.hpp"
template<class T> A<T>::A(T v) : val(v) {}
template<class T> A<T>::~A(void) {}
template<class T> T A<T>::getA(void) const {
return this->val;
}
test2.cpp
--------
#include <iostream>
#include "A.hpp"
using namespace std;
int main(void) {
A<int> sth(5);
return 0;
}
This compiles, but during linking it reports:
test2.o(.text+0x26): In function `main':
test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
[in-charge]()'
collect2: ld returned 1 exit status
However, if I implement everything in A.hpp, it works fine. Am I doing
something wrong or is this a compiler issue?
I am using gcc 3.3.5.
--
one's freedom stops where others' begin
Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/
I have written the following:
A.hpp
-----
#ifndef A_H
#define A_H
template<class T> class A {
private:
T val;
public:
A(T v);
~A(void);
T getA(void) const;
};
#endif
A.cpp
-----
#include "A.hpp"
template<class T> A<T>::A(T v) : val(v) {}
template<class T> A<T>::~A(void) {}
template<class T> T A<T>::getA(void) const {
return this->val;
}
test2.cpp
--------
#include <iostream>
#include "A.hpp"
using namespace std;
int main(void) {
A<int> sth(5);
return 0;
}
This compiles, but during linking it reports:
test2.o(.text+0x26): In function `main':
test2.cpp: undefined reference to `A<int>::A[in-charge](int)'
test2.o(.text+0x35):test2.cpp: undefined reference to `A<int>::~A
[in-charge]()'
collect2: ld returned 1 exit status
However, if I implement everything in A.hpp, it works fine. Am I doing
something wrong or is this a compiler issue?
I am using gcc 3.3.5.
--
one's freedom stops where others' begin
Giannis Papadopoulos
Computer and Communications Engineering dept. (CCED)
University of Thessaly
http://dop.freegr.net/