用C ++创建对象

时间:2012-04-30 20:34:12

标签: c++ oop object constructor

我是C ++的新手。这是我的问题。 我将总线,步进器声明为h文件中的私有实例:

class Motor_control
{
public:
       ...
private:
    IICBus bus ;
    IICStepper stepper ;
    IICStepper stepper_r ;
    IICStepper stepper_l ;
};

然后在构造函数

中启动它们
Motor_control::Motor_control(){
    IICBus bus ("/dev/i2c-2",0,"");
    IICStepper stepper (bus,0x00, "Global addr");
    IICStepper stepper_r (bus,0x6e, "Stepper Modul");
    IICStepper stepper_l (bus,0x66, "Stepper Modul");
}

IICStepper在.cpp:

中声明
 IICStepper::IICStepper(IICBus& bus, int addr, const string& name) : IICBase(bus,addr, name) { } in cpp

和.h:

class IICStepper : public IICBase { public: IICStepper(IICBus& bus, int addr, const std::string& name); virtual ~IICStepper(){}; ...} 

它compalins

       ../src/Motor_control.h:15: error: no matching function for call to 'IICStepper::IICStepper()'
../src/Stepper.h:41: note: candidates are: IICStepper::IICStepper(IICBus&, int, const std::string&)
../src/Stepper.h:38: note:                 IICStepper::IICStepper(const IICStepper&)
../src/Motor_control.h:15: error: no matching function for call to 'IICBus::IICBus()'
../src/Bus.h:28: note: candidates are: IICBus::IICBus(const std::string&, int, const std::string&)
../src/Bus.h:17: note:                 IICBus::IICBus(const IICBus&)
../src/Motor_control.h:15: error: no matching function for call to 'IICStepper::IICStepper()'
../src/Stepper.h:41: note: candidates are: IICStepper::IICStepper(IICBus&, int, const std::string&)
../src/Stepper.h:38: note:                 IICStepper::IICStepper(const IICStepper&)
../src/Motor_control.h:15: error: no matching function for call to 'IICStepper::IICStepper()'
../src/Stepper.h:41: note: candidates are: IICStepper::IICStepper(IICBus&, int, const std::string&)
../src/Stepper.h:38: note:                 IICStepper::IICStepper(const IICStepper&)
../src/Motor_control.h:15: error: no matching function for call to 'IICStepper::IICStepper()'
../src/Stepper.h:41: note: candidates are: IICStepper::IICStepper(IICBus&, int, const std::string&)
../src/Stepper.h:38: note:                 IICStepper::IICStepper(const IICStepper&)

3 个答案:

答案 0 :(得分:3)

在构造函数体进入之前,将初始化所有用户类型。因为您没有IICStepper的默认构造函数,所以您将收到错误。

您需要使用初始化列表:

Motor_control::Motor_control() : 
   bus("/dev/i2c-2", 0, ""),
   stepper(bus, 0, "Global addr"),
   stepper_r(bus, 0x6e, "Stepper Modul"),
   stepper_l(bus, 0x66, "Stepper Modul") 
{

}

您的版本不仅不会初始化成员,还会创建以后从未使用过的新临时对象。

答案 1 :(得分:0)

我最好的猜测是,当你传递一个字符串作为参数时,例如

"Global addr"

,字符串作为'const char *'传递,而不是'const std :: string&'。从我的角度来看,你只是传递了错误类型的变量。

答案 2 :(得分:0)

这是错误的:

Motor_control::Motor_control(){
    IICBus bus ("/dev/i2c-2",0,"");
    IICStepper stepper (bus,0x00, "Global addr");
    IICStepper stepper_r (bus,0x6e, "Stepper Modul");
    IICStepper stepper_l (bus,0x66, "Stepper Modul");
}

上面的代码没有按照您的想法执行。具体来说,它创建了三个局部变量(构造函数的本地变量),其名称与三个其他变量的名称完全相同 - 您的成员变量。此代码初始化您的成员变量。

您看到的错误消息是由成员变量的默认初始化引起的。要避免默认初始化,必须在初始化列表中列出成员变量,如下所示:

Motor_control::Motor_control() : bus("/dev/i2c-2", 0, ""),
    stepper(bus, 0, "Global addr"),
    stepper_r(bus, 0x6e, "Stepper Modul"),
    stepper_l(bus, 0x66, "Stepper Modul") {
}