0

I am really stuck at this problem for two days now. I know I am missing some crutial information but I don't know what.

I am programming an ESP32 using vscode and platformio.

I have imported ezButton and ezOutput. They both work fine when I call them from main.cpp. I assume this is because I am not calling them from inside a different class. Now I wrote a custom class which does some stuff and I want to read inputs from ezButton and write outputs to ezOutput.

Where my head is stuck at at this point is that I don't now how to correctly initialize the classes correctly.

I imagine it working like in my main.cpp where I for example just type inputPinName.isPressed();and I get my information. This should also work the other way like outputPinName.high();.

I would really appreciate someone explaining this to me or providing a link to a resource that explains this issue.

Here are some code snippets:

main.cpp

#include <ezButton.h>
#include <ezOutput.h>

// INPUTS
ezButton inputPinUp(34, INPUT_PULLDOWN);
//... same for all other inputs

// OUTPUTS
ezOutput outputPinUp(4);
ezOutput outputPinDown(0);

#define DEBOUNCE_TIME 50

void setup()
{
    inputPinUp.setDebounceTime(DEBOUNCE_TIME);
    //... same for all other pins
}

void loop()
{
    inputPinUp.loop();
    //... same for all other pins
}

shutter.h

#include <ezButton.h>
#include <ezOutput.h>

class shutter
{
private:
    int inputPinUp;
    //...
    int _outUp;
    int _outDown;

    ezButton Button_;
    ezOutput Output_;

public:
    shutter(ezOutput& outputPin) : Output_(outputPin) {} //ERROR: no default constructor exists for class "ezButton"
    shutter(ezButton& Pin) : Button_(Pin) {} //ERROR: no default constructor exists for class "ezOutput"

    void shutterInput(
      int inputPinUp,...);
    
    void shutterOutput(
      int outputPinUp,
      int outputPinDown);
  
    void stop(void);
    void driveUp(void);
    void driveDown(void);

};

I didn't switch the error messages above. They are actually reversed somehow.

shutter.cpp

#include "shutter.h"

//...

void shutter::shutterOutput(
  int outputPinUp,
  int outputPinDown)
{

  int _outUp = outputPinUp; //I am not sure if I even have to do this
}

void shutter::stop(void)
{
    ezOutput _outUp.low(); //ERROR: no default constructor exists for class "ezOutput"
    digitalWrite(_outDown, LOW);
    _moving = false;
}
Sebastian
  • 17
  • 5
  • 1
    `ezOutput _outUp.low();` attempts to be both a variable definition *and* a function call, both at the same time. Which of course won't work. You need to pick one or the other. Especially since `_outUp` is an `int` variable which doesn't even have member functions. What are you really trying to do there? – Some programmer dude Feb 17 '23 at 17:19
  • "I didn't switch the error messages above. They are actually reversed somehow." They don't look reversed to me, and I'm curious why you think they do look reversed. Your `shutter(ezOutput& outputPin)` constructor uses `outputPin` to initialize `Output_`, and then tries to default-initialize `Button_`. But `ezButton` doesn't have a default constructor, so the compiler tells you that your `shutter` constructor is invalid because it relies on a default constructor for `ezButton`. – Nathan Pierson Feb 17 '23 at 17:19
  • 1
    As for `int _outUp = outputPinUp;` that is indeed wrong, as you define a totally new and different variable named `_outUp` which shadows `this->_outUp` which I suppose you really should initialize or assign to? – Some programmer dude Feb 17 '23 at 17:21
  • @Someprogrammerdude thank you for your answer. I made `_outUp` an int because it should contain an integer aka the number of the pin. I have never heard about member functions. I will research into that. – Sebastian Feb 17 '23 at 17:29
  • 1
    Where are you learning about classes that don't explain the concept of member functions? It sounds like you need a [decent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to cover some fundamentals. – Nathan Pierson Feb 17 '23 at 17:30
  • @NathanPierson thanks for your answer. I understand that ezButton doesn't have a default constructor. But I don't understand why ` shutter(ezOutput& outputPin) : Output_(outputPin) {}` would have somesthing to do with `Button_`. – Sebastian Feb 17 '23 at 17:32
  • 1
    Definitely go with a book. C++ is very hard to learn by trial end error and it doesn't reward guesswork. – user4581301 Feb 17 '23 at 17:32
  • 1
    A `shutter` constructor has to initialize _all_ the member variables of the `shutter` class. That means a single constructor needs to initialize both `Button_` and `Output_`. If you don't specifically tell it how to initialize `Button_` (which yours doesn't), it will attempt to default-initialize `Button_`. Which it cannot do, and causes the error warning shown. – Nathan Pierson Feb 17 '23 at 17:33
  • Even though you initialize `Output_` (in that specific constructor), the other member variables needs to be constructed as well. – Some programmer dude Feb 17 '23 at 17:33
  • I bought C++ Primer but havn't read much yet. That would probably be a good idea. Didn't expect this to be so complicated. – Sebastian Feb 17 '23 at 17:34
  • 1
    Perhaps it's your design that is flawed, and you try to use a single class for too much? Perhaps you should split up the behavior into two or more classes? – Some programmer dude Feb 17 '23 at 17:34
  • Ok well. Thank you for your advice. I will definetly read a decent chunk of the book before I go to attemped to solve this issue. I now realise I have little idea what you are talking about. – Sebastian Feb 17 '23 at 17:38

0 Answers0