Code
(I've omitted much of the code, and left just the outline and code probably relating to the error.)
SelfDrivingCar.ino
#include "JKMain.h"
void setup() { JKMain::setup(); }
void loop() { JKMain::loop(); }
JKMain.h
#include "JKMotors.h"
class JKMain
{
static JKMotors motors;
public:
static void setup();
static void loop();
};
JKMain.cpp
#include "JKMain.h"
#include "JKPin.h"
const int leftMotorStraightSpeed = 100;
const int rightMotorStraightSpeed = 100;
const int leftMotorTurnSpeed = 150;
const int rightMotorTurnSpeed = 150;
const int turn90Time = 700;
void JKMain::loop() { }
void JKMain::setup()
{
motors(JKPin::LeftMotorSpeed, JKPin::LeftMotorDirectionBit1, JKPin::LeftMotorDirectionBit2,
JKPin::RightMotorSpeed, JKPin::RightMotorDirectionBit1, JKPin::RightMotorDirectionBit2,
leftMotorStraightSpeed, rightMotorStraightSpeed,
leftMotorTurnSpeed, rightMotorTurnSpeed, turn90Time);
}
JKMotors.h
class JKMotors
{
private:
const int leftMotorSpeedPin;
const int leftMotorDirectionBit1Pin;
const int leftMotorDirectionBit2Pin;
const int rightMotorSpeedPin;
const int rightMotorDirectionBit1Pin;
const int rightMotorDirectionBit2Pin;
const int leftMotorStraightSpeed;
const int rightMotorStraightSpeed;
const int leftMotorTurnSpeed;
const int rightMotorTurnSpeed;
const int turn90Time;
void setSideDirection(JKDirection _side, JKDirection _direction);
public:
JKMotors(const int lmsp, const int lmdb1p, const int lmdb2p,
const int rmsp, const int rmdb1p, const int rmdb2p,
const int lmss, const int rmss, const int lmts, const int rmts, const int t90t);
};
JKMotors.cpp
#include "JKMotors.h"
#include "Arduino.h"
JKMotors::JKMotors(const int lmsp, const int lmdb1p, const int lmdb2p,
const int rmsp, const int rmdb1p, const int rmdb2p,
const int lmss, const int rmss, const int lmts, const int rmts, const int t90t):
leftMotorSpeedPin(lmsp), leftMotorDirectionBit1Pin(lmdb1p), leftMotorDirectionBit2Pin(lmdb2p),
rightMotorSpeedPin(rmsp), rightMotorDirectionBit1Pin(rmdb1p), rightMotorDirectionBit2Pin(rmdb2p),
leftMotorStraightSpeed(lmss), rightMotorStraightSpeed(rmss), leftMotorTurnSpeed(lmts), rightMotorTurnSpeed(rmts), turn90Time(t90t)
{
pinMode(leftMotorSpeedPin, OUTPUT);
pinMode(leftMotorDirectionBit1Pin, OUTPUT);
pinMode(leftMotorDirectionBit2Pin, OUTPUT);
pinMode(rightMotorSpeedPin, OUTPUT);
pinMode(rightMotorDirectionBit1Pin, OUTPUT);
pinMode(rightMotorDirectionBit2Pin, OUTPUT);
}
JKPin.h
#include "Arduino.h"
struct JKPin
{
// Ultrasonic
static const int UltrasonicEcho = A4;
static const int UltrasonicTrig = A5;
// Servo
static const int ServoSignal = 10;
// Motor
static const int LeftMotorSpeed = 5;
static const int LeftMotorDirectionBit1 = 7;
static const int LeftMotorDirectionBit2 = 6;
static const int RightMotorSpeed = 3;
static const int RightMotorDirectionBit1 = 8;
static const int RightMotorDirectionBit2 = 9;
};
Notes
I wouldn't worry too much about the SelfDrivingCar.ino
file. I had it "encapsulated" with JKMain.h
. Also, don't concentrate on JKPins.h
, as it is mostly "factual" information likely unrelated to the problem.
Error
sketch/JKMain.cpp: In static member function 'static void JKMain::setup()':
JKMain.cpp:19: error: no match for call to '(JKMotors) (const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&)' leftMotorTurnSpeed, rightMotorTurnSpeed, turn90Time);
exit status 1
no match for call to '(JKMotors) (const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&, const int&)'
Request
As far as I see, I do indeed have the constructor that the error claims I lack. What's wrong with my code?
Update
(reply to jantje)
I when removing static
from motors
, I receive a new error:
In file included from sketch/JKMain.cpp:1:0:
sketch/JKMain.h: In static member function 'static void JKMain::setup()':
JKMain.h:10: error: invalid use of member 'JKMain::motors' in static member function
JKMotors motors;
JKMain.cpp:16: error: from this location
motors(JKPin::LeftMotorSpeed, JKPin::LeftMotorDirectionBit1, JKPin::LeftMotorDirectionBit2,
exit status 1
invalid use of member 'JKMain::motors' in static member function
static JKMotors motors;
why are you making a class when you appear to be planning to have a single instance of the class? A namespace might be more appropriate and less work.JKMain
orJKMotors
?JKMotors
, since you are making a static variablemotors
. If you do it like that, you have to then also make a static instance of the variable at global scope.