Arduino - C++ Object 构造函数错误
Arduino - C++ Object Constructor Error
我正在尝试调试我的一些 arduino 代码并不断收到与 object 构造函数相关的错误。我相信我已经正确定义了所有内容并提供了正确的输入,但我仍然无法编译代码。
我在下面粘贴了错误和代码。请注意,我已经删除了 'Data Control' class 中的一些源代码。另请注意,CDOS_Info 是我创建的结构。
感谢您的帮助!
错误:
Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp: In constructor 'StateController::StateController(CDOS_Info*)':
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:19:58: error: no matching function for call to 'DataControl::DataControl()'
StateController::StateController(CDOS_Info *inputtedInfo)
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:19:58: note: candidates are:
In file included from C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.h:16:0,
from C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:13:
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:19:3: note: DataControl::DataControl(CDOS_Info*)
DataControl(CDOS_Info *inputtedInfo);
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:19:3: note: candidate expects 1 argument, 0 provided
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:16:7: note: DataControl::DataControl(const DataControl&)
class DataControl
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:16:7: note: candidate expects 1 argument, 0 provided
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
代码
主要:
/***********************************************
*********** Import Libraries ******************
***********************************************/
#include "CDOS_Definitions.h"
#include "ConfigInit.h"
#include "SensorSerialInterface.h"
#include "StateController.h"
#include "OutputInterface.h"
#include <SoftwareSerial.h>
/***********************************************
******* Declare datatypes/objects *************
***********************************************/
CDOS_Info info; // Datatype holds general information regarding the CDOS
CDOS_Info * infoPointer = &info; // Declare and define a pointer of the CDOS_Info datatype
ConfigInit configInit(infoPointer); // Configures & initializes the CDOS
SensorSerialInterface sensorInterface(infoPointer); // Controls the communications between the Arduino and IMU
StateController stateController(infoPointer); // Transitions the CDOS between various states
OutputInterface outputMessage(infoPointer); // Outputted serial message providing status information to a computer terminal
/***********************************************
*************** Main Program *****************
***********************************************/
void setup(){
configInit.config();
configInit.init();
sensorInterface.setAllOutput(); // Force IMU to output status of accelerometer, gyroscope, and magnetometer
}
void loop(){
sensorInterface.readAll(info.accelXYZ, info.gyroXYZ, info.magXYZ); // Get sensor data
stateController.routine(); // Jump into state transition logic
outputMessage.outputSerial(); // Output CDOS status info
}
状态控制器Header:
#ifndef StateController_h
#define StateController_h
#include "CDOS_Definitions.h"
#include "InputInterface.h"
#include "DataControl.h"
#include "LEDController.h"
class StateController
{
public:
StateController(CDOS_Info *inputtedInfo);
void routine(void);
private:
CDOS_Info *info; // create copy of CDOS_Info pointer that member functions can access and modify
InputInterface userInput; // Create input interface object
DataControl dataControl; // Create data control object
LEDController lightController; // Controls the LEDs on the CDOS
};
#endif
状态控制器源代码:
#include "Arduino.h"
#include "CDOS_Definitions.h"
#include "StateController.h"
#include "LEDController.h"
#include "InputInterface.h"
#include "DataControl.h"
StateController::StateController(CDOS_Info *inputtedInfo)
{
info = inputtedInfo;
userInput = InputInterface();
dataControl = DataControl(info);
lightController = LEDController();
}
void StateController::routine(void)
{
int static previousTime;
int currentTime = millis();
bool static startTime = false;
int teachTime = 5000;
// State 0 - Start Up State
if(info->currentState == 0)
{
// Transition Case
if(userInput.getDigitalInput())
{
info->currentState = 3;
previousTime = currentTime;
}
// Lighting
lightController.setLightConfig0();
}
// State 1 - Disturbed State
else if(info->currentState == 1)
{
// Transition Case
if(userInput.getDigitalInput())
{
info->currentState = 3;
previousTime = currentTime;
}
// Compute vertical angle
dataControl.calculateVerticalAngle();
// Lighting
lightController.setLightConfig1();
}
// State 2 - Faulted State
else if(info->currentState == 2)
{
// Haven't created any cases where the system can fault yet, will finish later
// Lighting
lightController.setLightConfig2();
}
// State 3 - Teaching State
else if(info->currentState == 3)
{
// Transition Case
if(abs(currentTime - previousTime) > teachTime)
{
info->currentState = 4;
dataControl.teachNewPosition();
}
// Lighting
lightController.setLightConfig3();
}
// State 4 - Taught State
else if(info->currentState == 4)
{
// Transition Case
if(dataControl.checkPosition())
{
info->currentState = 1;
}
// Compute vertical angle
dataControl.calculateVerticalAngle();
// Lighting
lightController.setLightConfig4();
}
}
数据控制Header:
#ifndef DataControl_h
#define DataControl_h
#include "CDOS_Definitions.h"
class DataControl
{
public:
DataControl(CDOS_Info *inputtedInfo);
void teachNewPosition(void);
bool checkPosition(void);
void calculateVerticalAngle(void);
private:
CDOS_Info *info; // create copy of CDOS_Info pointer that member functions can access and modify
};
#endif
数据控件源代码:
#include "Arduino.h"
#include "DataControl.h"
#include "CDOS_Definitions.h"
#include "SensorSerialInterface.h"
DataControl::DataControl(CDOS_Info *inputtedInfo)
{
info = inputtedInfo;
}
您不能在不调用其构造函数的情况下创建对象。因为 StateController
也是 DataControl
,您不能在不调用 DataControl
的构造函数(以及 StateController
的)的情况下创建一个。但是那个构造函数有一个参数,所以编译器不能为你添加调用。
调用基 class 的构造函数的方法是像这样修改派生的 class 的构造函数:
StateController::StateController(CDOS_Info *inputtedInfo)
: DataControl(inputtedInfo) // calls DataControl's constructor
{
// info = inputtedInfo; // Don't need this, because DataControl's constructor does it.
userInput = InputInterface();
dataControl = DataControl(info);
lightController = LEDController();
}
注意奇怪的语法 - 这不是一个正常的函数调用。是的,它 确实 超出了大括号。
我正在尝试调试我的一些 arduino 代码并不断收到与 object 构造函数相关的错误。我相信我已经正确定义了所有内容并提供了正确的输入,但我仍然无法编译代码。
我在下面粘贴了错误和代码。请注意,我已经删除了 'Data Control' class 中的一些源代码。另请注意,CDOS_Info 是我创建的结构。
感谢您的帮助!
错误:
Arduino: 1.6.5 (Windows 7), Board: "Arduino Uno"
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp: In constructor 'StateController::StateController(CDOS_Info*)':
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:19:58: error: no matching function for call to 'DataControl::DataControl()'
StateController::StateController(CDOS_Info *inputtedInfo)
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:19:58: note: candidates are:
In file included from C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.h:16:0,
from C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\StateController.cpp:13:
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:19:3: note: DataControl::DataControl(CDOS_Info*)
DataControl(CDOS_Info *inputtedInfo);
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:19:3: note: candidate expects 1 argument, 0 provided
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:16:7: note: DataControl::DataControl(const DataControl&)
class DataControl
^
C:\Users\A5KW5ZZ\Desktop\arduino-1.6.5-r2-windows\arduino-1.6.5-r2\libraries\CDOS\DataControl.h:16:7: note: candidate expects 1 argument, 0 provided
Error compiling.
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
代码
主要:
/***********************************************
*********** Import Libraries ******************
***********************************************/
#include "CDOS_Definitions.h"
#include "ConfigInit.h"
#include "SensorSerialInterface.h"
#include "StateController.h"
#include "OutputInterface.h"
#include <SoftwareSerial.h>
/***********************************************
******* Declare datatypes/objects *************
***********************************************/
CDOS_Info info; // Datatype holds general information regarding the CDOS
CDOS_Info * infoPointer = &info; // Declare and define a pointer of the CDOS_Info datatype
ConfigInit configInit(infoPointer); // Configures & initializes the CDOS
SensorSerialInterface sensorInterface(infoPointer); // Controls the communications between the Arduino and IMU
StateController stateController(infoPointer); // Transitions the CDOS between various states
OutputInterface outputMessage(infoPointer); // Outputted serial message providing status information to a computer terminal
/***********************************************
*************** Main Program *****************
***********************************************/
void setup(){
configInit.config();
configInit.init();
sensorInterface.setAllOutput(); // Force IMU to output status of accelerometer, gyroscope, and magnetometer
}
void loop(){
sensorInterface.readAll(info.accelXYZ, info.gyroXYZ, info.magXYZ); // Get sensor data
stateController.routine(); // Jump into state transition logic
outputMessage.outputSerial(); // Output CDOS status info
}
状态控制器Header:
#ifndef StateController_h
#define StateController_h
#include "CDOS_Definitions.h"
#include "InputInterface.h"
#include "DataControl.h"
#include "LEDController.h"
class StateController
{
public:
StateController(CDOS_Info *inputtedInfo);
void routine(void);
private:
CDOS_Info *info; // create copy of CDOS_Info pointer that member functions can access and modify
InputInterface userInput; // Create input interface object
DataControl dataControl; // Create data control object
LEDController lightController; // Controls the LEDs on the CDOS
};
#endif
状态控制器源代码:
#include "Arduino.h"
#include "CDOS_Definitions.h"
#include "StateController.h"
#include "LEDController.h"
#include "InputInterface.h"
#include "DataControl.h"
StateController::StateController(CDOS_Info *inputtedInfo)
{
info = inputtedInfo;
userInput = InputInterface();
dataControl = DataControl(info);
lightController = LEDController();
}
void StateController::routine(void)
{
int static previousTime;
int currentTime = millis();
bool static startTime = false;
int teachTime = 5000;
// State 0 - Start Up State
if(info->currentState == 0)
{
// Transition Case
if(userInput.getDigitalInput())
{
info->currentState = 3;
previousTime = currentTime;
}
// Lighting
lightController.setLightConfig0();
}
// State 1 - Disturbed State
else if(info->currentState == 1)
{
// Transition Case
if(userInput.getDigitalInput())
{
info->currentState = 3;
previousTime = currentTime;
}
// Compute vertical angle
dataControl.calculateVerticalAngle();
// Lighting
lightController.setLightConfig1();
}
// State 2 - Faulted State
else if(info->currentState == 2)
{
// Haven't created any cases where the system can fault yet, will finish later
// Lighting
lightController.setLightConfig2();
}
// State 3 - Teaching State
else if(info->currentState == 3)
{
// Transition Case
if(abs(currentTime - previousTime) > teachTime)
{
info->currentState = 4;
dataControl.teachNewPosition();
}
// Lighting
lightController.setLightConfig3();
}
// State 4 - Taught State
else if(info->currentState == 4)
{
// Transition Case
if(dataControl.checkPosition())
{
info->currentState = 1;
}
// Compute vertical angle
dataControl.calculateVerticalAngle();
// Lighting
lightController.setLightConfig4();
}
}
数据控制Header:
#ifndef DataControl_h
#define DataControl_h
#include "CDOS_Definitions.h"
class DataControl
{
public:
DataControl(CDOS_Info *inputtedInfo);
void teachNewPosition(void);
bool checkPosition(void);
void calculateVerticalAngle(void);
private:
CDOS_Info *info; // create copy of CDOS_Info pointer that member functions can access and modify
};
#endif
数据控件源代码:
#include "Arduino.h"
#include "DataControl.h"
#include "CDOS_Definitions.h"
#include "SensorSerialInterface.h"
DataControl::DataControl(CDOS_Info *inputtedInfo)
{
info = inputtedInfo;
}
您不能在不调用其构造函数的情况下创建对象。因为 StateController
也是 DataControl
,您不能在不调用 DataControl
的构造函数(以及 StateController
的)的情况下创建一个。但是那个构造函数有一个参数,所以编译器不能为你添加调用。
调用基 class 的构造函数的方法是像这样修改派生的 class 的构造函数:
StateController::StateController(CDOS_Info *inputtedInfo)
: DataControl(inputtedInfo) // calls DataControl's constructor
{
// info = inputtedInfo; // Don't need this, because DataControl's constructor does it.
userInput = InputInterface();
dataControl = DataControl(info);
lightController = LEDController();
}
注意奇怪的语法 - 这不是一个正常的函数调用。是的,它 确实 超出了大括号。