【发布时间】:2011-02-22 14:27:22
【问题描述】:
我正在尝试通过使用受保护的虚拟成员函数 (http://www.parashift.com/c++-faq-lite/friends.html#faq-14.3) 来模拟虚拟朋友函数的效果。 此外,我为我的模板类使用了显式的特化。我想知道是否有人可以阐明我做错了什么。 下面,我发布了带有编译器错误的代码。欢迎任何建议!
foo.h
template < class T, class U >
class CFoo;
template < class T, class U >
void applyOnBar( const CFoo<T, U> &, const CBar<U> );
template < class T, class U >
class CFoo{
public:
.....
friend void applyOnBar< >( const CFoo<T, U> &, const CBar<U> & ); // error, line 28: function does not match any template declaration.
.....
virtual ~CFoo();
protected:
virtual void do_applyOnBar(const CBar<U> &);
.....
};
foo.cpp
template < class T, class U >
void applyOnBar( const CFoo<T, U> & refFoo, const CBar<U> & refBar ){
refFoo.do_applyOnBar(refBar); // error, line 36: passing argument first argument discards qualifiers.
}
template < class T, class U >
void CFoo<T, U>::do_applyOnBar( const CBar<U> & refBar ){ // error, line 40: member function is protected.
......
}
#include "../impl/foo-impl.inc"
foo-impl.inc
template class CFoo<float, float>; // line #1
.....
template void applyOnBar(const CFoo<float, float> &, const CBar<float> &); // line #6
./inc/foo.h: In instantiation of ‘CFoo<float, float>’:
./src/../impl/foo-impl.inc:1: instantiated from here
./inc/foo.h:28: error: template-id ‘applyOnBar<>’ for ‘void applyOnBar(const CFoo<float, float>&, const CBar<float>&)’ does not match any template declaration
./src/foo.cpp: In function ‘void applyOnBar(const CFoo<T, U>&, const CBar<U>&) [with T = float, U = float]’:
./src/../impl/foo-impl.inc:6: instantiated from here
./src/foo.cpp:40: error: ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ is protected
./src/foo.cpp:36: error: within this context
./src/foo.cpp:36: error: passing ‘const CFoo<float, float>’ as ‘this’ argument of ‘void CFoo<T, U>::do_applyOnBar(const CBar<U>&) [with T = float, U = float]’ discards qualifiers
【问题讨论】:
标签: c++ templates inheritance virtual