Solved! Simple class will not compile

I am trying to create a simple class, and I can't seem to get this code to compile. The code and the error message are below.

#ifndef bcd_input
#define bcd_input

#include <arduino.h>

class bcd_input
{
  private:
    int bit_1;
    int bit_2;
    int bit_3;
    int bit_4;

  public:
    bcd_input();
    int IntegerValue();
};

#endif
    
bcd_input::bcd_input();

int bcd_input::IntegerValue()
{
  int returnVal = (bit_4 << 3 | bit_3 << 2| bit_2 << 1 | bit_1);
  return returnVal;
};

Error Message:
bcd_input.cpp:17:15: error: expected unqualified-id before ')' token
bcd_input();
^
bcd_input.cpp:9:1: error: an anonymous struct cannot have function members
{
^
bcd_input.cpp:19:1: error: abstract declarator '' used as declaration
};

I expect it's your #define at the top having the same name as the class.

Capital "A"?
#include <Arduino.h>

and _h

#ifndef bcd_input_h
#define bcd_inpu_h

It is a good practice to use capital letters on include guards, so you avoid name conflicts with classes or variables. Also, you are declaring the constructor outside the class, but you should actually implement it. If you want an empty constructor, you can add brackets after the parenthesis.

#ifndef BCD_INPUT
#define BCD_INPUT

#include <Arduino.h>

class bcd_input
{
  private:
    int bit_1;
    int bit_2;
    int bit_3;
    int bit_4;

  public:
    bcd_input();
    int IntegerValue();
};

#endif
    
bcd_input::bcd_input() {};

int bcd_input::IntegerValue()
{
  int returnVal = (bit_4 << 3 | bit_3 << 2| bit_2 << 1 | bit_1);
  return returnVal;
};

This compiles on my environment. Anyway, I would separate the class declaration from the implementation, this is, having a .h and a .cpp file.

2 Likes

Each function should have a return type. If it returns nothing the type should be set to void.
bcd_input() has no type in your class definition.

Because it's a constructor...

Of course, you are right!

A Constructor Function is a member function under public: specifier (with its arguments) and is intended to initialize the member variables under private: specifier. It always comes with the same name of the class name. By virtue of C++’s mechanism, it is automatically called upon when a new object is created. The constructor function does not have any return data type even no void either. For example (to blink L of UNO):

class DigitalIO
{
  private:
    int ledPin;

  public:
    DigitalIO();     //default cnstructor
    void ioDir();
    void ledON();
    void ledOFF();
};

DigitalIO led;

void setup()
{
  Serial.begin(9600);
  led.ioDir();
}

void loop()
{
  led.ledON();
  delay(1000);
  led.ledOFF();
  delay(1000);
}

void DigitalIO::ioDir()
{
  pinMode(ledPin, OUTPUT);
}

void DigitalIO::ledON()
{
  digitalWrite(ledPin, HIGH);
}

void DigitalIO::ledOFF()
{
  digitalWrite(ledPin, LOW);
}

DigitalIO::DigitalIO()
{
  ledPin = 13;
}

The only error there is the missing brackets after the constructor implementation, as @saogalde already said.

Thanks for all the help. It was the #ifndef name matching the class name.

1 Like

Are those directives needed at all?

Glad to hear we helped! Please mark the reply that served you as solution :slight_smile: Happy coding!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.