C++ 派生类的模板专业化问题

C++ 派生类的模板专业化问题,c++,templates,C++,Templates,当我编译以下代码时 #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 << " >"

当我编译以下代码时

#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;
}
#包括
#定义XXX 1
#定义YYY 2
X类
{
公众:
模板
void func();
};
模板void X::func()
{

std::cout类Y中没有声明名为func()的方法,因此编译器找不到任何Y::func()声明您无法执行此操作,因为您也无法执行以下操作,并且出于相同的原因,
Y::func
未在
Y
中声明:

class X {
public: 
   void foo();
};
void X::foo() {}
class Y : public X {
};
void Y::foo() {}
class X {
public: 
   void foo();
};
void X::foo() {}
class Y : public X {
};
void Y::foo() {}