将对象引用作为参数传递给我错误:
No matching function for a call to i2cController::i2cController
我还尝试传递一个指针,并使用new创建i2cController。
两次尝试都给了我相同的结果。
为什么甚至抱怨调用i2cController,我是否不传递对象而不是调用它?
i2Controller(传递的对象)
i2cController.h
#pragma once
class i2cController
{
private:
int foo;
public:
i2cController(int Foo);
void write(int value);
};
i2cController.cpp
#include <i2cController.h>
i2cController::i2cController(int Foo)
{
foo = Foo;
}
void i2cController::write(int value)
{
foo++;
}
Led类(接收对象引用,源于错误)
错误源自:Led::Led(int pin, i2cController &Controller);
Led.h
#pragma once
#include <i2cController.h>
class Led
{
private:
i2cController controller;
int pin;
public:
Led(int pin, i2cController &Controller);
void turnOn();
};
led.cpp
#include <Led.h>
Led::Led(int Pin, i2cController &Controller)
{
controller = Controller;
pin = Pin;
}
void Led::turnOn()
{
controller.write(pin);
}
主要
i2cController controller(5);
Led led1 = new Led(led1Pin, controller);
答案 0 :(得分:0)
在您的类Led
中,您拥有controller
作为字段,并且没有初始化它,而是在Led
构造函数主体中使用了对其的赋值,因此必须首先默认构造它。解决方案可能是初始化它并制作副本:
Led::Led(int Pin, i2cController &Controller) :
controller( Controller ),
pin( Pin )
{
}
那你的问题就应该消失
答案 1 :(得分:0)
问题来自Led类的构造函数。构造函数正在尝试初始化默认的i2cController controller;
字段。构造函数也在寻找没有参数的默认i2cController构造函数,该参数未定义。
如果您需要参考,请按照以下说明进行操作。
class i2cController
{
private:
int foo;
public:
explicit i2cController(int Foo)
foo(Foo)
{}
void write(int value);
};
class Led
{
Led(Led&) = delete;
Led& operator=(Led&) = delete;
private:
i2cController &controller;
int pin;
public:
Led(int pin, i2cController &Controller);
~Led() noexcept;
void turnOn();
};
Led::Led(int Pin, i2cController &Controller)
controller(Controller),
pin(Pin)
{}
Led::~Led() noexcept
{}
如果您需要副本
class Led
{
private:
i2cController controller;
int pin;
public:
Led(int pin,const i2cController &Controller);
void turnOn();
};
Led::Led(int Pin,const const i2cController &Controller)
controller(Controller),
pin(Pin)
{}