The problem I am struggling with is the declaration of specialized template function inside template class (I keep class declaration in header file and define member functions in associated .C file).
I have template class representing Points. The header file is presented below:
//...
template<typename T, int dim=3> // T - coords. type, int dim - no. of dimensions
class Point {
public:
// ...
// function below sets val at the given position in array m_c and returns reference
template<int position> Point& set(T val);
private:
T m_c[dim]; // coordinates
};
//...
definition of function set
is placed in .C file:
template<typename T, int dim> template<int position> Point<T, dim>& Point<T, dim>::set(T val){
// ...
return *this;
}
As I understand this is the most general form of its definition.
In main function I create Point
with float
as T
and try to set some values in the array:
int main(int argc, char** argv) {
Point<float> p1;
p1.set<0>(3).set<1>(3.6).set<2>(3);
//...
}
In order to make this possible with definition of the member functions of template outside header file I need to inform compiler about specialization in .C file:
template class Point<float>;
and I need as well to declare usage of set function, which I try to accomplish this way (and this piece of code is the problem):
template<> template<int> Point<float>& Point<float>::set(float);
That unfortunately doesn't do the job and I get errors:
/tmp/ccR7haA5.o: In function `main':
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<0>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<1>(float)'
.../pdim.C:32: undefined reference to `Point<float, 3>& Point<float, 3>::set<2>(float)'
I would really appreciate an explanation from someone who may know how to cope with this problem. Thanks.