How to pass an array to a class as an argument

I created the Segments.h file and put the following code in it

#ifndef MY_7segments_H
#define MY_7segments_H

#include <Arduino.h>

class Segments {
  
  private:
    // bits representing segments A through G (and decimal point) for numerals 0-9
    const int numeral[10] = {
      //ABCDEFG /dp
      B11111100, // 0
      B01100000, // 1
      B11011010, // 2
      B11110010, // 3
      B01100110, // 4
      B10110110, // 5
      B10111110, // 6
      B11100000, // 7
      B11111110, // 8
      B11110110, // 9
    };
    int nbrDigits;
    int segmentPins;
    int digitPins;
    
  public:
    Segments(int nbrDigits, int segmentPins, int digitPins);
    void init();
    void showNumber(int number);
    void showDigit(int number, int digit);
};

#endif

Also created file Segments.cpp, and put the following code:

#include "7segments.h"

Segments::Segments(int nbrDigits, int segmentPins, int digitPins) {
  this->nbrDigits = nbrDigits;
  this->segmentPins = segmentPins;
  this->digitPins = digitPins;
  init();
}

Then I try to call my class from the main program, but I get a compilation error.
Could you please tell me how to correctly pass an array to a class?

const int segmentPins[] = { A9, A8, A7, A6, A5, A4, A3, A2 };
const int nbrDigits= 2; // the number of digits in the LED display
//dig 0 1 2 3
const int digitPins[nbrDigits] = { A0, A1 };
Segments myseg(nbrDigits, segmentPins, digitPins);

This

Segments(int nbrDigits, int segmentPins, int digitPins)

function has three arguments, all are of type int. The ones that are arrays should be so declared, either

Segments(int nbrDigits, int *segmentPins, int *digitPins)

or

Segments(int nbrDigits, int segmentPins[], int digitPins[])

depending on how old you are. :expressionless:

TBC, either works, both mean the same thing.

Although the const part may mess you up, that's how old I am.

a7

1 Like

@kazan116
Please, post your complete sketch including setup() and loop() functions to allow novice readers to understand the foundation/applicaton of C++ Language from your example. Also, indicate if you are using Arduino MEGA and the type of 7-segment display device (CC or CA).

why don't you provide a complete sketch with setup and loop we could put in our IDE and test?

That's an example of a class which could be used to hand over two arrays:

/*
   class with pointers to two arrays
*/


const uint8_t segmentPins[] = { A9, A8, A7, A6, A5, A4, A3, A2 };
const uint8_t digitPins[] = { A0, A1 };

template <size_t nbrDigits>  // the number of digits in the LED display
class Segments {
  private:
    // bits representing segments A through G (and decimal point) for numerals 0-9
    const uint8_t numeral[10] = {
      //ABCDEFG /dp
      0b11111100, // 0
      0b01100000, // 1
      0b11011010, // 2
      0b11110010, // 3
      0b01100110, // 4
      0b10110110, // 5
      0b10111110, // 6
      0b11100000, // 7
      0b11111110, // 8
      0b11110110, // 9
    };
    const uint8_t *segmentPins;
    const uint8_t *digitPins;
    
  public:
    Segments(const uint8_t (&segmentPins)[8], const uint8_t (&digitPins)[nbrDigits]) : segmentPins(segmentPins), digitPins(digitPins)  {}

    void debug() {
      Serial.println("segmentPins");
      for (size_t i = 0; i < 8; i++ ) {
          Serial.println(segmentPins[i]);
      }
      Serial.print("nbrDigits="); Serial.println(nbrDigits);
      Serial.println("digitPins");
      for (size_t i = 0; i < nbrDigits; i++) {
          Serial.println(digitPins[i]);
      }
    }
};

Segments <2>myseg(segmentPins, digitPins);

void setup() {
  Serial.begin(115200);
  myseg.debug();
}

void loop() {
  // put your main code here, to run repeatedly:
}
1 Like

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