【发布时间】:2014-08-08 19:23:25
【问题描述】:
尝试实例化SystemBody 或AcceleratedBody 类型的类时遇到以下错误:
error: 'bool InitializationChecker::is_initialized_' is protected
确实,is_initialized_ 是 protected。
class InitializationChecker {
[. . .]
protected:
bool is_initialized_;
};
is_initialized_ 通过其他几个类继承自 InitializationChecker。
InitializationChecker
^
|
MultipleInitializationChecker
^
|
Body
^
|
AcceleratedBody
^
|
SystemBody
Body 是这样声明的:
template<typename Type> class Body : public MultipleInitializationChecker {
using MultipleInitializationChecker::is_initialized_;
public:
Body() {
this->is_initialized_ = false;
}
Body(const Scalar<Type> mass__,
const CartesianVector<Type> position__,
const CartesianVector<Type> velocity__)
: mass_(mass__),
position_(position__),
velocity_(velocity__) {
this->is_initialized_ = true;
}
[. . .]
Scalar<Type> mass_;
CartesianVector<Type> position_;
CartesianVector<Type> velocity_;
};
我还遇到了另一个问题:SystemBody 的一些成员无法访问。例如:
error: 'Scalar<double> Body<double>::mass_' is inaccessible
当我尝试执行以下操作时输出:
SystemBody<double> system_body;
std::cout << system_body.mass_.value();
即使SystemBody 只添加了一个不相关的私有成员并且AcceleratedBody 被声明为这样:
template<typename Type> class AcceleratedBody : public Body<Type> {
using Body<Type>::is_initialized_;
using Body<Type>::mass_;
using Body<Type>::position_;
using Body<Type>::velocity_;
public:
AcceleratedBody() : Body<Type>::Body() {}
AcceleratedBody(const Scalar<Type> mass__,
const CartesianVector<Type> position__,
const CartesianVector<Type> velocity__,
const CartesianVector<Type> acceleration__)
: Body<Type>::Body(mass__, position__, velocity__),
acceleration_(acceleration__) {}
[. . .]
CartesianVector<Type> acceleration_;
};
请注意,mass_、position_ 和 velocity_ 都在 Body 中公开声明。
【问题讨论】:
-
请把它钉在一个感兴趣的地方(错误信息发生在哪里以及为什么你不期望它)。
-
@πάνταῥεῖ 最好发布代表我的课程的简单代码吗?
标签: c++ templates inheritance