下面是两个案例。
案例 1) Base->BaseIndirect->DerivedIndirect
案例 2) Base->Derived
在案例 2) 中,我可以使用 3 个符号调用基类的模板函数。在案例 1) 中,我只能使用其中一种表示法来调用基类的模板函数。而且,我无法使用任何符号调用 BaseIndirect 的模板函数 :(。我该如何解决这个问题?谢谢。
struct Base {
template<bool R> inline void fbase(int k) {};
};
template<class ZZ> struct BaseIndirect : Base {
template<bool R> inline void fbaseIndirect(int k) {};
};
template<class ZZ>
struct DerivedIndirect : BaseIndirect<ZZ> {
DerivedIndirect() {
this->fbase<true>(5); // gives error, line 13
fbase<true>(5); // gives error, line 14
Base::fbase<true>(5); // WORKS, line 15
this->fbaseIndirect<true>(5); // gives error, line 16
fbaseIndirect<true>(5); // gives error, line 17
BaseIndirect<ZZ>::fbaseIndirect<true>(5); // gives error, line 18
}
};
template<class ZZ>
struct Derived : Base {
Derived() {
this->fbase<true>(5); // WORKS
fbase<true>(5); // WORKS
Base::fbase<true>(5); // WORKS
}
};
int main() {
Derived<int> der;
DerivedIndirect<int> derIndirect;
};
编译错误
test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect()':
test.cpp:14: error: 'fbase' was not declared in this scope
test.cpp:17: error: 'fbaseIndirect' was not declared in this scope
test.cpp: In constructor 'DerivedIndirect<ZZ>::DerivedIndirect() [with ZZ = int]':
test.cpp:34: instantiated from herep
test.cpp:13: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
test.cpp:16: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'
test.cpp:18: error: invalid operands of types '<unresolved overloaded function type>' and 'bool' to binary 'operator<'