W
wakun
Hi there,
I am learning template programming. When testing the partial
specialization, I have some probelms
Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data[n][100][100];
CT() {...}
const T& get(int k, int i, int j)
{
return data[k][j];
}
};
I have a partial specialization verion as follow
template <typename T>
class CT<T, 1>
{
public:
T data[1][100][100];
const T& get(int i, int j)
{
return data[0][j];
}
};
In main, I have
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // ERROR !!!
}
I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?
Thanks in advance
I am learning template programming. When testing the partial
specialization, I have some probelms
Here is a full templated class
template <typename T, int n>
class CT
{
public:
T data[n][100][100];
CT() {...}
const T& get(int k, int i, int j)
{
return data[k][j];
}
};
I have a partial specialization verion as follow
template <typename T>
class CT<T, 1>
{
public:
T data[1][100][100];
const T& get(int i, int j)
{
return data[0][j];
}
};
In main, I have
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 5, 8) << endl; // OK! return what I want
cout << ct2.get(5, 8) << endl; // also OK
cout << ct2.get(0, 5, 8) << endl; // ERROR !!!
}
I wonder how this error come ! Both ct1 and ct2 come from a same class
except the different template parameter, why compiler complian no
get(k, i, j) is defined in partial specialization class? If I want a
partial specialization class with all members available, should I
rewrite all the code ?
Thanks in advance