Search code examples
c++11arduino-c++platformio

How to get rid of "undefined reference to 'Class::member'"? in conjunction with initialization of an array


When building my code I get the following "undefined reference"-errors, which I cannot get rid of. I've already tried several hints from stack overflow but nothing helps :-(. Maybe you have an idea?

I use VSCode with PlatformIO for an Arduino Uno on Mac OS.

in function `get7SegBitMap':

/Users/christian/Projekt/src/charmap7seg.cpp:70: undefined reference to 'Led7SegmentCharMap::bitMap'
/Users/christian/Projekt/src/charmap7seg.cpp:70: undefined reference to `Led7SegmentCharMap::bitMap' collect2: error: ld returned 1 exit status

The hierarchy is:

  • main.cpp includes ledmatrix.hpp
    • ledmatrix.cpp includes ledmatrix.hpp
    • ledmatrix.hpp includes charmap7seg.hpp
      • charmap7seg.cpp includes charmap7seg.hpp

charmap7seg.hpp

#pragma once
#include <Arduino.h>

class Led7SegmentCharMap {
private:
    static const uint8_t bitMap[];  // will be initialized in cpp-file
    uint8_t getCharMapIndex(const unsigned char outChar);
public:
    // Konstruktur
    Led7SegmentCharMap();
    // BitMap zur Darstellung auf der 7-Segment-Anzeige für outChar ermitteln
    uint8_t get7SegBitMap(const unsigned char outChar);
};


int set7SegValue(const LedMatrixPos pos, const uint8_t charBitMap);

charmap7seg.cpp

#include <Arduino.h>
#include <charmap7seg.hpp>

// Konstruktur
Led7SegmentCharMap::Led7SegmentCharMap() {
    uint8_t bitMap[] = {    ///< charMap contains bitmaps for 7-seg-displays
        //gfedcba
        0b0111111, ///<  "0": Segments f, e, d, c, b, a   --> bitMap[0]
        0b0000110, ///<  "1": Segments c, b               --> bitMap[1]
        0b1011011, ///<  "2": Segments g, e, d, b, a      --> bitMap[2]
        (...)
    }
    (void)bitMap; // to suppress the compiler warning "unused variable"
};


uint8_t Led7SegmentCharMap::get7SegBitMap(const unsigned char outChar) {
    return bitMap[getCharMapIndex(outChar)];  //                 <===== this is line 70
};

(...)

ledmatrix.hpp

#pragma once
#include <Arduino.h>
#include <charmap7seg.hpp>

class LedMatrix {
private:
    Led7SegmentCharMap charMap;
    (...)
public:
    Led7SegmentCharMap();  // Konstruktor
    uint8_t get7SegBitMap(const unsigned char outChar);
    void LedMatrix::display(const String outString);
    (...)

ledmatrix.cpp

#include <ledmatrix.hpp>
(...)
void LedMatrix::display(const String outString) {
    (...) // get a char out of outString --> outChar
    uint8_t charBitMap = charMap.get7SegBitMap(outChar); // get 7-seg-"bitmap"
    (...)
};
(...)

My expection is that all dependencies are fulfilled (which is not true regarding the error messages). I had some trouble with initializing the bitMap-array. Maybe the undefined reference error is related to that?


Solution

  • Today I found a hint here: here.

    With this I've got a compilable solution. In the cpp file it is necessary to move the bitMap declaration out of the constructor and do the initialization there. See new cpp file below.

    So the initial question

    How to get rid of "undefined reference to 'Class::member'"? in conjunction with initialization of an array

    was wrong and should be:

    How to declare an c-array in header file and define it in the cpp-file without specifying the size/dimension?

    charmap7seg.hpp

    class Led7SegmentCharMap {
    public:
        uint8_t get7SegBitMap(const unsigned char outChar);
    
    private:
        static const uint8_t bitMap[];      ///< Bitmap-Tabelle mit den Bitmustern
    };
    

    charmap7seg.cpp

    const uint8_t Led7SegmentCharMap::bitMap[] =  {    ///< charMap contains bitmaps for 7-seg-displays
                                ///< bzw. den Buchstaben darstellen.
            0b0111111, ///<  "0": Segmente f, e, d, c, b, a    --> bitMap[0]
            0b0000110, ///<  "1": Segmente c, b                --> bitMap[1]
            0b1011011, ///<  "2": Segmente g, e, d, b, a       --> bitMap[2]
    };
    
    (...)