Jonathan Mcdougall wrote:[color=blue]
>
wakun@wakun.com wrote:[color=green]
> > Hi there,
> > I am learning template programming. When testing the partial
> > specialization, I have some probelms
> > If I want a
> > partial specialization class with all members available, should I
> > rewrite all the code ?[/color]
>
> Yes.
>[/color]
OK. This time I consider inheritance
template <typename T, int n>
class Base
{
public:
T data[n][100][100];
Base() {...}
void show(void)
{
cout << "Hi!" << endl;
}
const T& get(int k, int i, int j)
{
return data[k][i][j];
}
const T& get2(int k, int i, int j)
{
return data[k][i][j];
}
};
template <typename T, int n>
class CT : public Base<T, n>
{
public:
CT() :Base<T, n> {...}
};
template <typename T>
class CT<T, 1> : public Base<T, 1>
{
public:
CT<T,1>() :Base<T, 1> {...}
const T& get2(int i, int j)
{
return data[0][i][j];
}
};
// main
int main(void)
{
CT<double, 2> ct1;
CT<double, 1> ct2;
cout << ct1.get(1, 2, 3) << endl; // OK, of course
cout << ct1.show() << endl; // OK, of course
cout << ct2.show() << endl; // No problem, show is defined
in Base
cout << ct2.get(0, 1, 2) << endl; // OK. get is kept in both
CT<double, n> and CT<double, 1>
cout << ct2.get2(3, 2) << endl; // OK. overload function
ctou << ct2.get2(0, 1, 2) << endl; // ERROR!!! No such function!?
return 0;
}
Why compiler cannot find get2?