Scope problem with methods

I'm learning C++ and have a simple class to operate a 7-segment display. The code was compiling cleanly and then I broke it. I don't know what is broken. It acts like some of the methods are out-of-scope when called from other methods.

Using IDE 2.3.2 on a UNO R board.


#include "arduino.h"
/*  Display7Seg
test program to control a seven segment display using a class and methods

class provides methods to turn on and off a specific segment,
map the segment display pins to Arduino pins
test the pin mapping
display a specific digit
control the decimal point

segments and the segment pins are:
  ---A(7)--
  F(9)    B(6)
  ---G(10)--
  E(1)    C(4)
  ---D(2)-- and DP(5) [decimal point]

  pins 3 and 8 are ground
  

*/


class segmentDisplay {
  //  Define pins and globals

private:

  boolean segPinsInitialized = false;  // have the pins been assigned and set to output?
  boolean segEnableDP = false;         // is decimal point turned on

  int ArduinoPinMap[8];  // store the 8 Arduino pins attached to the display.
                         // there are 20 possible pins that can be used.
                         // these are A0 to A5 and 0 to 13

  //  define the Segment Display segments (A to G and DP)
  //    and corresponding pins on the display (1 to 10)
  //  The pins are defined in the hardware data sheet
  //  pins 3 and 8 are the ground pins

  int const segA = 7;
  int const segB = 6;
  int const segC = 4;
  int const segD = 2;
  int const segE = 1;
  int const segF = 9;
  int const segG = 10;
  int const segDP = 5;

  // PRIVATE METHODS

  int segAddSeg(int);   // ADD segment to those displayed
  int segDropSeg(int);  // REMOVE segment from those displayed

public:

  // define the PUBLIC methods

  int segAllOn();            // Turn ON all segments
  int segAllOff();           // Turn OFF all segments
  int segDisplayDigit(int);  // Display a digit
  int segDisplaySeg(int);    // Turn on single segment
  int segTestDisplay(int);   // Run a test pattern on display
  int segEnableDecimal();    // allow the decimal point
  int segDisableDecimal();   // turn off the decimal point

  // segMapPins -- Assign segment pins to Arduino pins. Returns 0 for success, 1 to 8 for which argument fails first
  int segMapPins(int, int, int, int, int, int, int, int);  // segment pins A - G and DP in that order

};  //end of CLASS segmentDisplay

segmentDisplay digitOne;

int segmentDisplay::segMapPins(int sPinA, int sPinB, int sPinC, int sPinD, int sPinE, int sPinF, int sPinG, int sPinDP) {

  // need to test the input arguments for valid values.  ArduinoPinMap sPinX values must be between 0 and 20, no duplicates ArduinoPinMapowed

  int returnValue = 0;  // define the return value

  // put the values into a GLOBAL array to facilitate the testing code

  ArduinoPinMap[0] = sPinA;  //  in order A-G and DP
  ArduinoPinMap[1] = sPinB;
  ArduinoPinMap[2] = sPinC;
  ArduinoPinMap[3] = sPinD;
  ArduinoPinMap[4] = sPinE;
  ArduinoPinMap[5] = sPinF;
  ArduinoPinMap[6] = sPinG;
  ArduinoPinMap[7] = sPinDP;

  for (int i = 0; i < 8; ++i) {  // loop through ArduinoPinMap of the input values now in the array

    if (ArduinoPinMap[i] < 0 || ArduinoPinMap[i] > NUM_DIGITAL_PINS) {  // Arduino pin outside range
      returnValue = i + 1;                                              // identify the incorrect argument
      return returnValue;                                               // return fail
    }

    // duplicate check

    for (int j = i + 1; j < 7; ++j) {              // test the current value (i) against ArduinoPinMap following values for duplicates
      if (ArduinoPinMap[i] == ArduinoPinMap[j]) {  // we have a duplicate -- fail with a -arg number
        returnValue = -i - 1;
        return returnValue;
      }
    }
    // end of validation FOR loop

    // the inputs are good -- set the pins as OUTPUT
    for (int i = 0; i < 7; ++i) {
      pinMode(ArduinoPinMap[i], OUTPUT);
    }
    segPinsInitialized = true;  // flag to later steps that pins are initialized
    return 0;                   // return success
  }
}  // End function segMapPins


int segmentDisplay::segTestDisplay(int NumCycles = 3) {
  // Assumes the pins are mapped

  int returnValue = 0;        // default to OK
  int k = 0;                  // test a theory
  if (!segPinsInitialized) {  // pins are not defined and set to output
    returnValue = 1;          // return fail
    return returnValue;
  }

  for (int j = 1; j < NumCycles; ++j) {

    k = segAllOn();
    delay(100);
    segAllOff();
    for (int i = 0; i < 8; ++i) {  // test individual segments

      segDisplaySeg(i);  // display the segment
      delay(50);         // show result
    }                    // end of for i

    for (int i = 0; i < 10; ++i) {  // show each digit
      segDisplayDigit(i);
      delay(100);
    }  // end of for i
  }    // end of for j
}  // end of segTestDisplay

int segmentDisplay::segDisplayDigit(int digitToDisplay) {  // display a digit 0 to 9

  int returnValue = 0;
  int A = 1, B = 2, C = 3, D = 4, E = 5, F = 6, G = 7, DP = 8;  // name segments
  int k = 0;
  if (digitToDisplay < 0 || digitToDisplay > 9) {
    returnValue = 1;  // error
    return returnValue;
  }  // endif

  k = segAllOff();
  if (segEnableDP) {
    segAddSeg(DP);  // turn on the decimal point
  }                 // endif

  switch (digitToDisplay)

  {
    case 0:
      segAllOn();
      segDropSeg(G);  // turn off cross bar, segment G
      break;
    case 1:
      segAddSeg(B);
      segAddSeg(C);
      break;
    case 2:
      segAllOn();
      segDropSeg(F);
      segDropSeg(C);
      break;
    case 3:
      segAllOn();
      segDropSeg(F);
      segDropSeg(E);
      break;
    case 4:
      segAddSeg(F);
      segAddSeg(B);
      segAddSeg(G);
      segAddSeg(C);
      break;
    case 5:
      segAllOn();
      segDropSeg(B);
      segDropSeg(E);
      break;
    case 6:
      segAllOn();
      segDropSeg(B);
      segDropSeg(E);
      break;
    case 7:
      segAddSeg(A);
      segAddSeg(B);
      segAddSeg(C);
      break;
    case 8:
      segAllOn();
      break;
    case 9:
      segAllOn();
      segDropSeg(E);
      break;
    default:
      returnValue = 1;  // this should NEVER happen
      break;
  }  // end switch
}  // End of segDisplayDigit

int segmentDisplay::segAddSeg(int segToDisplay) {
  int returnValue = 0;

  if (segToDisplay < 1 || segToDisplay > 8) {  // test for argument in bounds
                                               // the array containing the segment pin assignments is 0 to 7, 7 = DP

    returnValue = 1;
  } else {
    digitalWrite(ArduinoPinMap[segToDisplay - 1], HIGH);  // segments are 1 to 7 + DP
  }                                                       // endif
  return returnValue;
}  // end segAddSeg

int segmentDisplay::segDropSeg(int segToDisplay) {
  int returnValue = 0;
  if (segToDisplay < 1 || segToDisplay > 8) {  // test for argument in bounds
                                               // the array containing the segment pin assignments is 0 to 7, 7 = DP

    returnValue = 1;
  } else {
    digitalWrite(ArduinoPinMap[segToDisplay - 1], LOW);  // segments are 1 to 7 + DP
  }                                                      // endif
  return returnValue;
}  // end segDropSeg

void setup() {
  Serial.begin(9600);  // access serial monitor and initialize to 9600 baud.
  Serial.println("calling segMapPins. Return = ");
  Serial.println(digitOne.segMapPins(1, 2, 3, 4, 5, 6, 7, 8));
  Serial.print("Calling Test Display, return = ");
  Serial.println(digitOne.segTestDisplay());
  delay(100);
  Serial.print("Calling display digit for value 8, return = ");
  Serial.println(digitOne.segDisplayDigit(8));
  delay(100);
}

void loop() {
}

Compiler errors:

C:\Users\glenn\AppData\Local\Temp\cc1uneCR.ltrans0.ltrans.o: In function `segDisplayDigit':
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:156: undefined reference to `segmentDisplay::segAllOff()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:204: undefined reference to `segmentDisplay::segAllOn()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:165: undefined reference to `segmentDisplay::segAllOn()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:173: undefined reference to `segmentDisplay::segAllOn()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:178: undefined reference to `segmentDisplay::segAllOn()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:189: undefined reference to `segmentDisplay::segAllOn()'
C:\Users\glenn\AppData\Local\Temp\cc1uneCR.ltrans0.ltrans.o:C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:204: more undefined references to `segmentDisplay::segAllOn()' follow
C:\Users\glenn\AppData\Local\Temp\cc1uneCR.ltrans0.ltrans.o: In function `segTestDisplay':
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:132: undefined reference to `segmentDisplay::segAllOff()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:135: undefined reference to `segmentDisplay::segDisplaySeg(int)'
C:\Users\glenn\AppData\Local\Temp\cc1uneCR.ltrans0.ltrans.o: In function `segDisplayDigit':
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:156: undefined reference to `segmentDisplay::segAllOff()'
C:\Users\glenn\OneDrive\Documents\Arduino\Display7Seg/Display7Seg.ino:207: undefined reference to `segmentDisplay::segAllOn()'
collect2.exe: error: ld returned 1 exit status

exit status 1

Compilation error: exit status 1

Where have you implemented these functions?

Thank you. I had originally named the functions allOn() and allOff(). In converting from basically C code to a class I renamed things and didn't copy the two functions over as methods.

I was so tied up in looking for a problem with the class I overlooked problems with the code.

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