-2

我是 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(){}; ...} 

它比较

       ../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&)
4

3 回答 3

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") 
{

}

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

于 2012-04-30T20:35:39.643 回答
0

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

"Global addr"

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

于 2012-04-30T20:39:04.647 回答
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") {
}
于 2012-04-30T20:53:04.387 回答