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

时间:2013-07-03 17:04:46

标签: c++ enums

我定义了以下基类:

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类型的对象。 它调用超类CDeviceSetActiveRequest构造函数,传入请求类型的枚举值。 它最终调用CDeviceClientRequest设置私有成员变量的枚举值。

但是代码拒绝编译,提供以下错误消息:

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()in CDeviceSetActiveRequest.cpp
  • CDeviceTSSetActiveRequest.cpp为空,只包含标题。

以上2个cpp文件,使用getter方法,编译没有任何问题。它只是发生错误的第3个。

2 个答案:

答案 0 :(得分:1)

您忘记使用公共继承,如下所示:

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

答案 1 :(得分:0)

问题是你是私下继承你的类,所以中产阶级隐藏了枚举。

您可能希望在此公开继承:class CDeviceSetActiveRequest : public CDeviceClientRequestclass CDeviceTSSetActiveRequest : public CDeviceSetActiveRequest(注意我在那里添加了public一词)。