gpt4 book ai didi

c++ - 如何将基类的枚举传递给子类的构造函数

转载 作者:行者123 更新时间:2023-11-28 07:32:06 25 4
gpt4 key购买 nike

我定义了以下基类:

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方法。

  • CDeviceClientRequest.cpp 中的 CDeviceClientRequest::getMessageId()
  • CDeviceSetActiveRequest::getActivFlag() 在 CDeviceSetActiveRequest.cpp 中
  • CDeviceTSSetActiveRequest.cpp 是空的,只包含 header 。

上面2个cpp文件,有getter方法,编译没有问题。只有第 3 个发生错误。

最佳答案

你忘了使用公共(public)继承,像这样:

class CDeviceSetActiveRequest : public CDeviceClientRequest
/* ... */
class CDeviceTSSetActiveRequest : public CDeviceSetActiveRequest

关于c++ - 如何将基类的枚举传递给子类的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17454086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com