作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我定义了以下基类:
class CDeviceClientRequest
{
public:
/** Enumeration for the client message identifier. */
enum EClientMessageIdentifier
{
TS_SET_ACTIVE, // Set Active request for Total Station device.
GPS_SET_ACTIVE, // Set Active request for GPS device.
TS_SET_COM_PORT, // Set COM Port request for Total Station device.
GPS_SET_COM_PORT, // Set COM Port request for GPS device.
TS_GET_OBSERVATION, // Get Observation request for Total Station device.
GPS_GET_POSITION, // Get Position request for GPS device.
GPS_GET_QUALITY // Get Quality request for GPS device.
}; // EClientMessageIdentifier
/**
* Getter method for the client message identifier.
*/
EClientMessageIdentifier getMessageId();
protected:
/**
* Protected constructor.
*
* @param messageId
* The enumeration representing the request message identifier.
*/
CDeviceClientRequest(EClientMessageIdentifier messageId)
: m_iMessageId(messageId) {}
private:
/** The enumeration representing the request message identifier. */
EClientMessageIdentifier m_iMessageId;
}; // CDeviceClientRequest
它有以下子类:
class CDeviceSetActiveRequest : CDeviceClientRequest
{
public:
/**
* Getter method for the activation flag.
*/
bool getActiveFlag();
protected:
/**
* Protected constructor.
*
* @param messageId
* The enumeration representing the request message identifier.
* @param setActive
* The flag for the activation request.
*/
CDeviceSetActiveRequest(EClientMessageIdentifier messageId,
bool setActive)
: CDeviceClientRequest(messageId),
m_bActive(setActive) {}
private:
/** Flag for the activation request. */
bool m_bActive;
}; // CDeviceSetActiveRequest
它还有以下可实例化的子类:
class CDeviceTSSetActiveRequest : CDeviceSetActiveRequest
{
public:
/**
* Public constructor.
*
* @param setActive
* The flag for the activation request.
*/
CDeviceTSSetActiveRequest(bool setActive)
: CDeviceSetActiveRequest(CDeviceClientRequest::TS_SET_ACTIVE,
setActive) {}
}; // CDeviceTSSetActiveRequest
因此,您创建了一个类型为 CDeviceTSSetActiveRequest 的对象,并传入一个 bool 参数。它调用父类(super class) CDeviceSetActiveRequest 构造函数,传入请求类型的枚举值。它最终调用 CDeviceClientRequest 设置私有(private)成员变量的枚举值。
但是代码拒绝编译并给出以下错误信息:
camd011> make
g++ -c CDeviceTSSetActiveRequest.cpp \
-I.. -o bin/CDeviceTSSetActiveRequest.o
In file included from CDeviceTSSetActiveRequest.cpp:13:0:
CDeviceClientRequest.h: In constructor 'device::request::CDeviceTSSetActiveRequest::CDeviceTSSetActiveRequest(bool)':
CDeviceClientRequest.h:30:1: error: 'class device::request::CDeviceClientRequest device::request::CDeviceClientRequest::CDeviceClientRequest' is inaccessible
CDeviceTSSetActiveRequest.h:37:34: error: within this context
make: *** [bin/CDeviceTSSetActiveRequest.o] Error 1
有人可以帮忙吗?如何在可实例化的子类中使用我的主基类中定义的枚举?
忘记补充:文件的cpp版本实现了getter方法。
上面2个cpp文件,有getter方法,编译没有问题。只有第 3 个发生错误。
最佳答案
你忘了使用公共(public)继承,像这样:
class CDeviceSetActiveRequest : public CDeviceClientRequest
/* ... */
class CDeviceTSSetActiveRequest : public CDeviceSetActiveRequest
关于c++ - 如何将基类的枚举传递给子类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17454086/
我是一名优秀的程序员,十分优秀!