I'm a bit new to using classes in c++ and I'm having a bit of confusion when creating a class that will instantiate a few objects that I've already created.
For my example I'll keep it as bare bones as possible. I'm starting with a class called DigiPlat, this is a board that will have multiple switches, knobs, and footswitches on it. The first class I'm trying to add is the Footswitch class. FootSwitch hpp and cpp look as follows.
FootSwitch.cpp
class FootSwitch {
public:
FootSwitch(uint8_t pin_value, FSType fs_type);
void init_FootSwitch();
void update_FootSwitch();
// Add your class members here
FSType switch_type;
uint8_t pin;
uint8_t ButtonState;
uint8_t lastButtonState;
unsigned long lastDeboundeTime;
unsigned long debounceDelay = 50;
};
and
FootSwitch.cpp
FootSwitch::FootSwitch(uint8_t pin_value, FSType fs_type) {
// Initialize any variables or objects here
switch_type = fs_type;
pin = pin_value;
};
Now in the DigiPlat class I've created the class
DigiPlat.hpp
#include "FootSwitch.hpp"
class DigiPlat {
public:
// Constructor
DigiPlat();
//DigiPlat Variables
bool State;
FootSwitch FootSwitch1;
};
and the cpp file looks like
DigiPlat.cpp
DigiPlat::DigiPlat(){
FootSwitch FootSwitch1(FS1, NC);
};
However it won't compile like this. It seems as if it is not able to call the FootSwitch constructor. I'm curious on how to properly go about initializing FootSwitch1 properly. It will soon be followed by another FootSwitch and multiple other knobs and switches so keep that in mind.
I was not able to formulate good prompts to find a similiar question that would have answered this. Thanks for taking your time to answer the question. Cheers.
There's a syntax for that:
DigiPlat::DigiPlat()
: FootSwitch1(FS1, NC)
{}
Some people move the colon to the end of the first line. I like this style, because it's easy to add new lines by starting with a comma.
As a side note, you might consider your naming conventions. It's not clear why FootSwitch
is a class when FootSwitch1
is a member.