Includes not working

Hi guys. I am new to arduino and today I set up visual studio code to work with my arduino (uno). I am now trying to create a class but I am running on to some problems with the includes.

I have a MyButton.cpp that looks like this:

#include <MyButton.h>

class MyButton
{
  public:
    MyButton(int pin)
    {
      this->pin = pin;
    }

    void poll()
    {
      previousState = state;
      state = digitalRead(pin);
    }

    bool isPressedDown()
    {
      return previousState == 1 && state == 0;
    }

    bool isReleased()
    {
      return previousState == 0 && state == 1;
    }
};

and a MyButton.h that looks like this

class MyButton {
  private: 
    int pin;
    int state;
    int previousState;

  public: 
    MyButton(int pin);
    void poll();
    bool isPressedDown();
    bool isReleased();
};

In MyButton.cpp, it does not seem to recognize the include, as all variables declared in MyButton.h are red underlined indicating an error. The error is
class "Button" has no member "pin" and it is the same for all other variables.

Does anybody have an idea on what the problem could be?

  • Note: I am not new to programming but I am completely new to c++

Button is not the same thing as MyButton.

The name of the constructor should be the same as the class name

Good catch! I fixed that but I still have the same problem.

I can't see your code, I can't see your error message(s)

My code looks like this now

MyButton.cpp

#include "MyButton.h"

class MyButton
{
  public:
    MyButton(int pin)
    {
      this->pin = pin;
    }

    void poll()
    {
      previousState = state;
      state = digitalRead(pin);
    }

    bool isPressedDown()
    {
      return previousState == 1 && state == 0;
    }

    bool isReleased()
    {
      return previousState == 0 && state == 1;
    }
};

MyButton.h

class MyButton {
  private: 
    int pin;
    int state;
    int previousState;

  public: 
    MyButton(int pin);
    void poll();
    bool isPressedDown();
    bool isReleased();
};

The error:
class "MyButton" has no member "pin"

MyButton.cpp

#include "MyButton.h"
#include <Arduino.h>

MyButton::MyButton (int pin)     // consider to use byte
{
  this->pin = pin;               // don't do this. Use a initializer list!
}

void MyButton::poll()
{
  previousState = state;
  state = digitalRead(pin);
}

bool MyButton::isPressedDown()
{
  return previousState == 1 && state == 0;
}

bool MyButton::isReleased()
{
  return previousState == 0 && state == 1;
}

compiles with my secret user sketch without error/warning.

btw:

  • check all your int variables ... do you really need a signed two byte variable for values like 0 or 1?
  • which of your variables will never change? which one could be const?
  • check which variable type digitalRead is returning.
  • check what digitalRead is returning (spoiler: it returns the makro HIGH or LOW).
1 Like

Thanks for the feedback. I really appreciate it! I guess I have a lot to learn about C++. Though I am still encountering the same problem. Do you have any idea what could cause this?

The errors:


And when I try to upload the code to my arduino I get this error:

MyButton.cpp:4:7: error: redefinition of 'class MyButton'
 class MyButton
       ^~~~~~~~
In file included from /Users/lucdrenth/Desktop/Dev/Arduino/buttons/buttons.ino:1:0:
MyButton.cpp:4:7: error: redefinition of 'class MyButton'
 class MyButton
       ^~~~~~~~
In file included from /Users/lucdrenth/Desktop/Dev/Arduino/buttons/MyButton.cpp:1:0:
/Users/lucdrenth/Desktop/Dev/Arduino/buttons/MyButton.h:1:7: note: previous definition of 'class MyButton'
 class MyButton {
       ^~~~~~~~
In file included from /Users/lucdrenth/Desktop/Dev/Arduino/buttons/MyButton.cpp:1:0,
                 from /Users/lucdrenth/Desktop/Dev/Arduino/buttons/buttons.ino:1:
/Users/lucdrenth/Desktop/Dev/Arduino/buttons/MyButton.h:1:7: note: previous definition of 'class MyButton'
 class MyButton {
       ^~~~~~~~
exit status 1
IntelliSense configuration already up to date. To manually rebuild your IntelliSense configuration run "Cmd+Alt+I"
[Error] Uploading sketch 'buttons.ino': Exit with code=1

read very carefully what the compiler is telling you

MyButton.cpp:4:7: error: redefinition of 'class MyButton'

you have redefined the class

MyButton.h:1:7: note: previous definition of 'class MyButton'
 class MyButton {
       ^~~~~~~~

it was already definied in MyButton.h

check against my .cpp

1 Like

Ah that's it, thank you so much, your help and feedback is much appreciated! I'll mark this as solved :slight_smile: