I have been writing a Pong game for Arduino using capacitive buttons, and I have been getting a “no matching function for call to ‘CapacitiveSensor::CapacitiveSensor()'” error.. Here is the code for the class I’ve been the error with:
//Input.cpp
#include <CapacitiveSensor.h>
#include "Input.h"
#include "Arduino.h"
Input::Input (byte sPin, byte rPin1, byte rPin2) { //I get the error on this line
upButton = CapacitiveSensor(sPin, rPin1);
downButton = CapacitiveSensor(sPin, rPin2);
}
//Continued for bChk
And then this is the header:
//Input.h
#ifndef Input_H
#define Input_H
#include "Arduino.h"
#include <CapacitiveSensor.h>
class Input{
public:
const static byte up = 0;
const static byte down = 1;
CapacitiveSensor upButton;
CapacitiveSensor downButton;
boolean bChk(byte button);
Input(byte sPin, byte rPin1, byte rPin2);
};
#endif
I know standard naming conventions say that constants should be capitalized, but they are already reserved. And also that variables and such should be private. I’m lazy. Also, I get the error on a different line than the one that I call the constructor with… I didn’t make the CapacitiveSensor class, either.
I’m on a Mac, if it matters (I doubt it).
Looks like the
CapacitiveSensorclass does not define a default constructor, therefore theInputclass doesn’t know how to constructupButtonanddownButton.An option is to initialise
upButtonanddownButtonin theInputconstructor, passing the required arguments:Another option is to transform those members in pointers, and use
newto allocate them with the required arguments. Example:Or ultimately, you could extend the
CapacitiveSensorclass, define a default constructor, and pass fixed arguments to the super constructor. I believe that is not what you want, but anyway: