[C++] "Undefined Reference to " error

Yomega96
Posts: 5
Joined: Wed Oct 07, 2020 1:08 pm

[C++] "Undefined Reference to " error

Postby Yomega96 » Wed Dec 08, 2021 2:15 pm

Hi to all,
I'm having a problem with pointers.
I'm trying to create a custom Touch Keyboard library, using MCUFRIEND_kbv TFT library.
When I try to pass the pointer of the TFT instance (found in main.cpp as tft variable) to the drawKeys function of the Keyboard library (call in loop() of main.cpp as kb.drawKeys(&tft)) i get an error
  1. .pio\build\esp32doit-devkit-v1\src\main.cpp.o:(.literal._Z4loopv+0x4): undefined reference to `Keyboard::drawKeys(MCUFRIEND_kbv*)'
  2. .pio\build\esp32doit-devkit-v1\src\main.cpp.o: In function `loop()':
  3. Filepath/src/main.cpp:140: undefined reference to `Keyboard::drawKeys(MCUFRIEND_kbv*)'
"main.cpp" file
  1. //INCLUDES
  2. #include <SPI.h>
  3. #include "Adafruit_GFX.h"
  4. #include <MCUFRIEND_kbv.h>
  5. #include <Wire.h>
  6. #include "Fonts/FreeSerif24pt7b.h"
  7. #include <SD.h>
  8. #include "Keyboard.h"
  9.  
  10. //TFT
  11. MCUFRIEND_kbv tft;
  12.  
  13. //KeyBoard & Buttons configuration
  14. #define XELEMENTS 13
  15. #define YELEMENTS 3
  16. #define BUTTONMARGINS 1
  17. #define SIDEPADDING 6
  18. int _width = round(tft.width()/XELEMENTS)-(2*BUTTONMARGINS);
  19. int _height = _width;
  20. Keyboard kb(_height, _width, YELEMENTS, XELEMENTS, BUTTONMARGINS, SIDEPADDING);
  21.  
  22. bool sd_init = false;
  23. void setup()
  24. {
  25.     Serial.begin(250000);
  26.     Serial.println(F("TFT LCD test"));
  27.     pinMode(27, INPUT);
  28.     pinMode(22, OUTPUT);
  29.     Serial.print("TFT size is ");
  30.     Serial.print(tft.width());
  31.     Serial.print("x");
  32.     Serial.println(tft.height());
  33.     tft.reset();
  34.     Serial.print("ID: ");
  35.     Serial.println(tft.readID(), HEX);
  36.     uint16_t identifier = 0x9486;
  37.     tft.begin(identifier);
  38.     tft.fillScreen(TFT_BLACK);
  39.     tft.setRotation(3);
  40.     if (SD.begin(5))
  41.     {
  42.         sd_init = true;
  43.     }
  44.     tft.fillRect(0, 0, 480, 320, TFT_BLACK);
  45.    
  46. }
  47. void loop(void)
  48. {
  49.     while(1)
  50.     {
  51.         //tft.setFont(&FreeSerif24pt7b);
  52.         tft.fillRect(0,50,480,100, TFT_BLACK);
  53.         kb.drawKeys(&tft);
  54.         delay(2000);
  55.     }
  56. }
"Keyboard.h" file
  1. #ifndef _KEYBOARD_H
  2. #define _KEYBOARD_H
  3.  
  4. #include "Adafruit_GFX.h"
  5. #include <MCUFRIEND_kbv.h>
  6.  
  7. class Keyboard {
  8.     private:
  9.  
  10.     public:
  11.         Keyboard(short _h, short _w, short _yel, short _xel, short _bmargins, short _sidepad);
  12.         void drawKeys(MCUFRIEND_kbv* _tft);
  13. };
  14. #endif
"Keyboard.cpp" file
  1. #include "Keyboard.h"
  2. #include "MCUFRIEND_kbv.h"
  3. #include "Adafruit_GFX.h"
  4.  
  5. //Defines to configurate buttons
  6. /*
  7. #define XELEMENTS 13
  8. #define YELEMENTS 3
  9. #define BUTTONMARGINS 1
  10. #define SIDEPADDING 6
  11. */
  12.  
  13. short xEl = 0, yEl = 0, bMargins = 0, sidePadding = 0, width = 0, height = 0;
  14. unsigned short bBorder = 0, bBackground = 0, bTextColor = 0;
  15.  
  16. const char Mobile_KB[3][13] PROGMEM = {
  17. {0, 13, 10, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
  18. {2, 11, 9, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'},
  19. {2, 11, 7, 'Z', 'X', 'C', 'V', 'B', 'N', 'M'},
  20. };
  21.  
  22. const char Mobile_NumKeys[3][13] PROGMEM = {
  23. {0, 13, 10, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
  24. {2, 11, 9, '-', '/', ':', ';', '(', ')', '&', '@', '"'},
  25. {2, 11, 7, '.', ',', '?', '!', '\'', '`', '$'}
  26. };
  27.  
  28. const char Mobile_SymKeys[3][13] PROGMEM = {
  29. {0, 13, 10, '[', ']', '{', '}', '#', '%', '^', '*', '+', '='},
  30. {2, 11, 9, '_', '\\', '|', '~', '<', '>', '$', '"', '&'}, //4
  31. {2, 11, 7, '.', ',', '?', '!', '\'', '`', '@'}
  32. };
  33.  
  34. Keyboard::Keyboard(short _h, short _w, short _yel, short _xel, short _bmargins, short _sidepad)
  35. {
  36.     height = _h;
  37.     width = _w;
  38.     yEl = _yel;
  39.     xEl = _xel;
  40.     bMargins = _bmargins;
  41.     sidePadding = _sidepad;
  42. }
  43.  
  44. void drawKeys(MCUFRIEND_kbv &_tft)
  45. {
  46.     int yoffset = height - (height * yEl);
  47.     for (int i = 0; i < yEl; i++)
  48.     {
  49.         for (int x = 0; x < xEl; x++)
  50.         {
  51.             _tft.fillRoundRect(((width*x)+bMargins+sidePadding),(((height*i)+yoffset)+bMargins), width-(2*bMargins), height-(2*bMargins), 8, TFT_DARKCYAN);
  52.             _tft.drawRoundRect(((width*x)+bMargins+sidePadding),(((height*i)+yoffset)+bMargins), width-(2*bMargins), height-(2*bMargins), 8, TFT_CYAN);
  53.         }
  54.     }
  55. }
  56.  
  57.  
I'm unable to compile, and unable to find the problem 😥

Thanks to whoever explains me how to try to solve the problem
Kind regards

chegewara
Posts: 2375
Joined: Wed Jun 14, 2017 9:00 pm

Re: [C++] "Undefined Reference to " error

Postby chegewara » Wed Dec 08, 2021 7:32 pm

Code: Select all

void drawKeys(MCUFRIEND_kbv &_tft) <----------------ERROR in this line
{
    int yoffset = height - (height * yEl);

Yomega96
Posts: 5
Joined: Wed Oct 07, 2020 1:08 pm

Re: [C++] "Undefined Reference to " error

Postby Yomega96 » Wed Dec 08, 2021 8:36 pm

chegewara wrote:

Code: Select all

void drawKeys(MCUFRIEND_kbv &_tft) <----------------ERROR in this line
{
    int yoffset = height - (height * yEl);
I've solved!

Code: Select all

void Keyboard::drawKeys(MCUFRIEND_kbv* _tft)
What a noob,
Declaration with type* var , definition with type &var

Thanks for the help
Yomega96

Who is online

Users browsing this forum: Baidu [Spider] and 137 guests