下面是一个简单的类层次结构,其中混合了模板化和非模板化类。嵌入在这个例子的评论是我想解决的编译错误。混合模板/非模板继承分类和成员继承
我在这里阅读了一些其他相关的线索,并尝试了几种形式的“使用”关键字无济于事。
我在这些试验中了解到: 父:: parent_ivar_ = 99; 是可行的语法,但不知道如何告诉编译器关于我想在Child类中设置/使用的继承的ivar grandparent_ivar_。
在此先感谢!
class GrandParent {
public:
int grandparent_ivar_;
};
template <typename T>
class Parent : public GrandParent {
public:
int parent_ivar_;
};
template <typename T>
class Child : public Parent<T> {
public:
Child() {
// The following stmt produces this compiler error using ubuntu g++:
// templateinheritance.cpp: In constructor ‘Child<T>::Child()’:
// templateinheritance.cpp:20:5: error: ‘grandparent_ivar_’ was
// not declared in this scope
// grandparent_ivar_ = 100;
//
grandparent_ivar_ = 100;
}
};
int main(int argc, char *argv[]) {
Child<int> c;
}
谢谢你的好信息山姆。是的,我错过了这个例子中的公共继承。我认为这与问题没有密切关系。我会研究这个 - > foo解决方案。随着一些进一步的调查“使用GrandParent :: grandparent_ivar;”放置在子类声明中也起作用。干杯。 – user501138