2010-08-13 70 views
0

我编译以下问题的模板专业化的派生类

#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 

我想专注于一个基类的模板。

任何人都可以解释它是如何完成的或为什么我不能这样做。

Thx Mark。

回答

1

你不能这样做,因为你不能做以下任,为相同的原因,Y::func未在Y中声明:

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

Doh !!。我是多么愚蠢的人...... – ScaryAardvark 2010-08-13 13:38:50

1

没有与名称func()的Y类中声明没有方法,所以编译器无法找到任何Ÿ:: FUNC()声明