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

标签 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 的对象,并传入一个 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/

相关文章:

c++ - 有可变参数模板的替代品吗

c - 在宏中使用枚举整数值来格式化字符串

c++ - 无法在 C++ 中定义++ 运算符,这里有什么问题?

c++ - 设置 SSE 寄存器中的最后或前 n 位

c++ - 自动生成 move 构造函数

c++ - C++ 中的锁和互斥锁

c++ - 模板函数给出 "no matching function for call"错误

以枚举作为参数的 Qt 信号

java - 根据类成员选择合适的枚举类

java - 使用枚举而不是集合的好处