当我编译以下内容时
#include <iostream>
#define XXX 1
#define YYY 2
class X
{
public:
template< int FLD >
void func();
};
template<> void X::func< XXX >()
{
std::cout << "X::func< " << XXX << " >" << std::endl;
}
class Y : public X
{
public:
};
template<> void Y::func< YYY >()
{
std::cout << "Y::func< " << YYY << " >" << std::endl;
}
template<> void Y::func< XXX >()
{
std::cout << "Y::func< " << XXX << " >" << std::endl;
}
int main( int c, char *v[] )
{
X x;
Y y;
}
我明白了
x.cpp:24: error: template-id 'func<2>' for 'void Y::func()' does not match any template declaration
x.cpp:24: error: invalid function declaration
x.cpp:29: error: template-id 'func<1>' for 'void Y::func()' does not match any template declaration
x.cpp:29: error: invalid function declaration
我正在尝试在基类中专门化模板。
任何人都可以解释它是如何完成的或者为什么我不能这样做。
谢谢 马克。
最佳答案
您不能这样做,因为您也不能执行以下操作,出于同样的原因,Y::func
未在 Y
中声明:
class X {
public:
void foo();
};
void X::foo() {}
class Y : public X {
};
void Y::foo() {}
关于c++ - 派生类的模板特化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3477174/