template<class T>
class Array
{
public:void mf(); #1
};
template class Array<char>; // explicit instantiation #2
template void Array<int>::mf(); // explicit instantiation #3
void main()
{
Array<double> a; // implicit instantiation
// my question is how to call mf() in #2 (explict declaration)from main()
}发布于 2010-04-28 20:38:56
这个问题有点不清楚,所以如果我弄错了,很抱歉。
您可以在显式实例化上调用函数,就像在隐式实例化上一样。
Array<char> c;
c.mf();要实现这一点,必须在显式实例化Array<char>时提供Array<T>::mf()的定义,或者定义Array<char>::mf()的专门化。因此,如果您满足以下任一条件,上述代码都将正常工作:
template <typename T> void Array<T>::mf() {cout << "Hello\n";}
template class Array<char>;或
template <> void Array<char>::mf() {cout << "Hello\n";}发布于 2010-04-28 20:36:48
a.mf();(如果您真的不知道,Stackoverflow不是您需要的。在here上寻求帮助。)
https://stackoverflow.com/questions/2729248
复制相似问题