Problem encountered when creating instance

Hi guys, I am a beginner in programming and I have been trying to create my own library for controlling a four digit seven segment display. The problem is that when I was trying to import the library and create an instance from the class from the library in trial.ino, the compiler gave me an error message

//
trial:3: error: expected initializer before ‘Display’

Four_dig_7_seg_display Display;

^

C:\Users\XXX\Documents\Arduino\Sketches\clock_humidity_temp_gadget\trial\trial.ino: In function ‘void loop()’:

trial:11: error: ‘Display’ was not declared in this scope

Display.displayNum();

^

Using library four_dig_7_seg_display in folder: C:\Users\XXX\Documents\Arduino\Sketches\clock_humidity_temp_gadget\trial\libraries\four_dig_7_seg_display (legacy)
exit status 1
expected initializer before ‘Display’
//

I was not able to find any problems with my codes in my library. I would really appreciate if somebody could help me out on my codess. Thanks very muchhhh:)

four_digit_7_seg_display.cpp

#include "Arduino.h"
#include "four_dig_7_seg_display.h"

Four_dig_7_seg_display::Four_dig_7_seg_display(int A, int B, int C, int D, int E, int F, int G, int H, int I, int J, int K, int L, int displayNumber) {
  int i = 1;
  while (i << 14) {
    pinMode (i, OUTPUT);
    digitalWrite(i,LOW);
    i++;
  }
  pinA = A;
  pinB = B;
  pinC = C;
  pinD = D;
  pinE = E;
  pinF = F;
  pinG = G;
  pinDP = H;
  d1 = I;
  d2 = J;
  d3 = K;
  d4 = L;
  _displayNumber = displayNumber;
}

void Four_dig_7_seg_display::displayNum () {
  switch (digit(_displayNumber)){
    case 1: 
      digitalWrite(d1,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      select(_displayNumber);
      delay(10);
      nth();
      digitalWrite(d1,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      C();
      delay(10);
      nth();
      break;
    case 2:
      digitalWrite(d1,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      select(_displayNumber%10);
      delay(10);
      nth();
      digitalWrite(d1,LOW);
      digitalWrite(d2,LOW);
      digitalWrite(d3,HIGH);
      digitalWrite(d4,LOW);
      select((_displayNumber-_displayNumber%10)/10);
      delay(10);
      nth();
      digitalWrite(d1,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      C();
      delay(10);
      nth();
      break;
    case 3:
      digitalWrite(d1,LOW);
      digitalWrite(d2,HIGH);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      select(_displayNumber%10);
      delay(10);
      nth();
      digitalWrite(d1,LOW);
      digitalWrite(d2,LOW);
      digitalWrite(d3,HIGH);
      digitalWrite(d4,LOW);
      select((_displayNumber%100-_displayNumber%10)/10);
      delay(10);
      nth();
      digitalWrite(d1,LOW);
      digitalWrite(d2,LOW);
      digitalWrite(d3,LOW);
      digitalWrite(d4,HIGH);
      select((_displayNumber-_displayNumber%100)/100);
      delay(10);
      nth();
      digitalWrite(d1,HIGH);
      digitalWrite(d2,LOW);
      digitalWrite(d3,LOW);
      digitalWrite(d4,LOW);
      C();
      delay(10);
      nth();
      break;
      default: break;
      }
}

int Four_dig_7_seg_display::digit(int num) {
  if (num/10 < 1) {
    return 1;
    }
  else 
  {
    if(num/10<10) {
      return 2;
    }
    else
     {if(num/10<100) {
      return 3;
      }
      else {return 0;}
     }
  }
}
 
void Four_dig_7_seg_display::select(int i) {
  switch (i) {
    case 1: one(); break;
    case 2: two(); break;
    case 3: three(); break;
    case 4: four(); break;
    case 5: five(); break;
    case 6: six(); break;
    case 7: seven(); break;
    case 8: eight(); break;
    case 9: nine(); break;
    case 0: zero(); break;
    default: break;
  }
}

void Four_dig_7_seg_display::one(void) {
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinA,LOW);
  digitalWrite(pinD,LOW);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,LOW);
  digitalWrite(pinG,LOW);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::two() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,LOW);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,HIGH);
  digitalWrite(pinF,LOW);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::three() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,LOW);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::four() {
  digitalWrite(pinA,LOW);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,LOW);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::five() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,LOW);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::six() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,LOW);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,HIGH);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::seven() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,LOW);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,LOW);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::eight() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,HIGH);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::nine() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,HIGH);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::zero() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,HIGH);
  digitalWrite(pinC,HIGH);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,HIGH);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,LOW);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::nth() {
  digitalWrite(pinA,LOW);
  digitalWrite(pinB,LOW);
  digitalWrite(pinC,LOW);
  digitalWrite(pinD,LOW);
  digitalWrite(pinE,LOW);
  digitalWrite(pinF,LOW);
  digitalWrite(pinG,LOW);
  digitalWrite(pinDP,LOW);
}

void Four_dig_7_seg_display::C() {
  digitalWrite(pinA,HIGH);
  digitalWrite(pinB,LOW);
  digitalWrite(pinC,LOW);
  digitalWrite(pinD,HIGH);
  digitalWrite(pinE,HIGH);
  digitalWrite(pinF,HIGH);
  digitalWrite(pinG,LOW);
  digitalWrite(pinDP,HIGH);
}

four_dig_7_seg_display.h

#ifndef four_dig_7_seg_display_h
#define four_dig_7_seg_display_h

#include "Arduino.h"

class Four_dig_7_seg_display
{
  public:
    Four_dig_7_seg_display(int A, int B, int C, int D, int E, int F, int G, int H, int I, int J, int K, int L, int displayNumber);
    void displayNum();
  private:
    int i;
    int pinA; // 6
    int pinB; //7
    int pinC; //8
    int pinD; //9
    int pinE; //10
    int pinF; //11
    int pinG; //12
    int pinDP; //13
    int d1; //5
    int d2; //4
    int d3; //3
    int d4; //2
    void one();
    void two();
    void three();
    void four();
    void five();
    void six();
    void seven();
    void eight();
    void nine();
    void zero();
    void nth();
    void C();
    void select(int i);
    int digit (int num);
    int _displayNumber; ;     
};

#endif

trial.ino

#include <four_dig_7_seg_display.h>

Four_dig_7_seg_display Display;

void setup() {


}

void loop() {
  Display.displayNum();

}

Thanks very muchhhh :))))

Try replacing < > by " " with your include:

#include "four_dig_7_seg_display.h"

As a point of answering "why ?’, <> instructs the linker to check the library paths as defined by your installation for the particular file. Double quotes specifies a directory which, with nothing else but the header file name, defaults to the directory that also contains the source code.

thx very much for the helppp:) i changed the <> into “” but the same error still remains. Is there anything more I can do? once again thx very much for all of ur helpp TT

Your object is lack of all the arguments in the constructor.

#include <four_dig_7_seg_display.h>

Four_dig_7_seg_display Display;

The class name and file name are supposed to match.

There_really_is_no_excuse_for_underscores_in_class_names.

ThereIsNoExcuseForRidiculouslyLongClassNames,either.

Thanks very much for all of your helpp :slight_smile: Problem finally solved. I found out that somehow the save function of the file has some problems and the things I edited and saved on the file opened by pressing the "open recent" button in the arduino interface werent really saved. Only if I opened the file from my explore could I save the edits. Anyways, thx for all ur helpp :)))

PaulS:

#include <four_dig_7_seg_display.h>

Four_dig_7_seg_display Display;



The class name and file name are supposed to match.

There_really_is_no_excuse_for_underscores_in_class_names.

ThereIsNoExcuseForRidiculouslyLongClassNames,either.

That is NOT a requirement and it will NOT cause the error mentioned. It is a common convention, but it will not cause any error. It causes annoyance to PaulS, but not the compiler.

Having a distinct function for every single digit is an INSANE way to code for a 7 segment. Look here:

byte digits[] = {0b1111110 , 0b0110000, 0b1101101, 0b1111001, 0b0110011, 0b1011011, 0b1011111, 0b1110000, 0b1111111, 0b1111011};

// pins to segments g,f,e,d,c,b,a
byte pins[] = {3,4,5,6,7,8,9};

void displayNumber(int aNumber) {  
  for (int i = 0; i < 7; i++) {
    digitalWrite(pins[i], digits[aNumber] & (1 << i));
  }
}

void setup(){
  for (int i = 0; i < 7; i++){
    pinMode(pins[i], OUTPUT);
  }
}


void loop(){
  for (int i = 0; i < 10; i++) {
    displayNumber(i);
    delay(1000);
  }
}

That counts up to 0 to 9. But do you see how it handles keeping up with what leds to light for each digit? Much simpler.