2

我有一个带有 2 个公共构造函数的类,我想调用一个私有构造函数:

class CDeviceTSGetObservationResponse : public CDeviceServerResponse
{
public:
   /**
    * Public constructor. Used to construct a response containing
    * the required information from the TotalStation device.
    *
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(double horizontalAngle, 
                                   double verticalAngle, 
                                   double slopeDistance)
       : CDeviceTSGetObservationResponse(CDeviceServerResponse::SUCCESS_MESSAGE,
                                         horizontalAngle,
                                         verticalAngle,
                                         slopeDistance) {}

   /**
    * Public constructor. Used to construct a response containing
    * the error message from the TotalStation device.
    *
    * @param errorMsg
    *           The error message for the response.
    */
   CDeviceTSGetObservationResponse(std::string errorMsg)
       : CDeviceTSGetObservationResponse(errorMsg,
                                         0.0,
                                         0.0,
                                         0.0) {}

private:
   /**
    * Private constructor.
    *
    * @param errorMsg
    *           The error message for the response.
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(std::string errorMsg, 
                                   double      horizontalAngle, 
                                   double      verticalAngle, 
                                   double      slopeDistance) 
       : CDeviceServerResponse(CDeviceServerResponse::TS_GET_OBSERVATION, 
                               errorMsg),
         m_dHorizontalAngle(horizontalAngle), 
         m_dVerticalAngle(verticalAngle), 
         m_dSlopeDistance(slopeDistance){}

   /** The horizontal angle. */
   double m_dHorizontalAngle;

   /** The vertical angle. */
   double m_dVerticalAngle;

   /** The slope distance. */
   double m_dSlopeDistance;
}; // CDeviceTSGetObservationResponse

因此,如果没有问题,用户将调用构造函数并传递三个布尔值,并且基类上的错误消息将默认为成功。

或者他们调用传入错误消息的构造函数,这会将值默认为 0.0。

我以为我可以用上面的代码做到这一点,但我收到以下错误消息:

camd011> make
g++ -c CDeviceTSGetObservationResponse.cpp \
        -I.. -o bin/CDeviceTSGetObservationResponse.o
In file included from CDeviceTSGetObservationResponse.cpp:13:0:
CDeviceTSGetObservationResponse.h: In constructor 'device::response::CDeviceTSGetObservationResponse::CDeviceTSGetObservationResponse(double, double, double)':
CDeviceTSGetObservationResponse.h:44:10: error: type 'device::response::CDeviceTSGetObservationResponse' is not a direct base of 'device::response::CDeviceTSGetObservationResponse'
CDeviceTSGetObservationResponse.h:47:55: error: no matching function for call to 'device::response::CDeviceServerResponse::CDeviceServerResponse()'
CDeviceTSGetObservationResponse.h:47:55: note: candidates are:
CDeviceServerResponse.h:72:4: note: device::response::CDeviceServerResponse::CDeviceServerResponse(device::response::CDeviceServerResponse::EServerMessageIdentifier, std::string)
CDeviceServerResponse.h:72:4: note:   candidate expects 2 arguments, 0 provided
CDeviceServerResponse.h:31:7: note: device::response::CDeviceServerResponse::CDeviceServerResponse(const device::response::CDeviceServerResponse&)
CDeviceServerResponse.h:31:7: note:   candidate expects 1 argument, 0 provided
CDeviceTSGetObservationResponse.h: In constructor 'device::response::CDeviceTSGetObservationResponse::CDeviceTSGetObservationResponse(std::string)':
CDeviceTSGetObservationResponse.h:57:10: error: type 'device::response::CDeviceTSGetObservationResponse' is not a direct base of 'device::response::CDeviceTSGetObservationResponse'
CDeviceTSGetObservationResponse.h:57:42: error: 'errorMeg' was not declared in this scope
CDeviceTSGetObservationResponse.h:60:45: error: no matching function for call to 'device::response::CDeviceServerResponse::CDeviceServerResponse()'
CDeviceTSGetObservationResponse.h:60:45: note: candidates are:
CDeviceServerResponse.h:72:4: note: device::response::CDeviceServerResponse::CDeviceServerResponse(device::response::CDeviceServerResponse::EServerMessageIdentifier, std::string)
CDeviceServerResponse.h:72:4: note:   candidate expects 2 arguments, 0 provided
CDeviceServerResponse.h:31:7: note: device::response::CDeviceServerResponse::CDeviceServerResponse(const device::response::CDeviceServerResponse&)
CDeviceServerResponse.h:31:7: note:   candidate expects 1 argument, 0 provided
CDeviceTSGetObservationResponse.cpp: In member function 'void device::response::CDeviceTSGetObservationResponse::serialize(Archive&, unsigned int)':
CDeviceTSGetObservationResponse.cpp:38:14: error: 'base_object' is not a member of 'boost::serialization'
CDeviceTSGetObservationResponse.cpp:38:69: error: expected primary-expression before '>' token
make: *** [bin/CDeviceTSGetObservationResponse.o] Error 1
4

3 回答 3

7

您正在寻找的是委托构造函数,而这些构造函数仅在 C++11 中可用。

例如:

struct A
{
    int x;

    A(): A(10) {}

private:    
    A(int val): x(val) {}
};

int main(int argc, char* argv[])
{
    A a;

    return 0;
}

正如您在此处看到的,它使用 C++11 编译,但如果您在此处查看相同的代码,则无法编译(在 C++11 之前)

于 2013-07-03T17:55:08.283 回答
1

最好提供一个普通的私有方法而不是私有构造函数,并在其他构造函数定义的主体中调用这个方法。

当然,您不能将成员初始化器列表用于此类私有方法,但在方法主体中根据需要初始化所有成员也可以。

更新:
除此之外,您的错误消息表明您在某些需要默认构造函数的上下文中使用您的类。

于 2013-07-03T17:49:57.683 回答
-1

好的,我删除了私有构造函数,并像这样重写了两个公共构造函数:

   /**
    * Public constructor. Used to construct a response containing
    * the required information from the TotalStation device.
    *
    * @param horizontalAngle
    *           The horizontal angle.
    * @param verticalAngle
    *           The vertical angle.
    * @param slopeDistance
    *           The slope distance.
    */
   CDeviceTSGetObservationResponse(double horizontalAngle, 
                                   double verticalAngle, 
                                   double slopeDistance)
       : CDeviceServerResponse(CDeviceServerResponse::TS_GET_OBSERVATION, 
                               CDeviceServerResponse::SUCCESS_MESSAGE),
         m_dHorizontalAngle(horizontalAngle), 
         m_dVerticalAngle(verticalAngle), 
         m_dSlopeDistance(slopeDistance) {}

   /**
    * Public constructor. Used to construct a response containing
    * the error message from the TotalStation device.
    *
    * @param errorMsg
    *           The error message for the response.
    */
   CDeviceTSGetObservationResponse(std::string errorMsg)
       : CDeviceServerResponse(CDeviceServerResponse::TS_GET_OBSERVATION, 
                               errorMsg),
         m_dHorizontalAngle(0.0), 
         m_dVerticalAngle(0.0), 
         m_dSlopeDistance(0.0) {}
于 2013-07-04T08:52:29.163 回答