0

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

.pio\build\esp32doit-devkit-v1\src\main.cpp.o:(.literal._Z4loopv+0x4): undefined reference to `Keyboard::drawKeys(MCUFRIEND_kbv*)'
.pio\build\esp32doit-devkit-v1\src\main.cpp.o: In function `loop()':
Filepath/src/main.cpp:140: undefined reference to `Keyboard::drawKeys(MCUFRIEND_kbv*)'

"main.cpp" file

//INCLUDES
#include <SPI.h>
#include "Adafruit_GFX.h"
#include <MCUFRIEND_kbv.h>
#include <Wire.h>
#include "Fonts/FreeSerif24pt7b.h"
#include <SD.h>
#include "Keyboard.h"
 
//TFT
MCUFRIEND_kbv tft;
 
//KeyBoard & Buttons configuration
#define XELEMENTS 13
#define YELEMENTS 3
#define BUTTONMARGINS 1
#define SIDEPADDING 6
int _width = round(tft.width()/XELEMENTS)-(2*BUTTONMARGINS);
int _height = _width;
Keyboard kb(_height, _width, YELEMENTS, XELEMENTS, BUTTONMARGINS, SIDEPADDING);
 
bool sd_init = false;
void setup()
{
    Serial.begin(250000);
    Serial.println(F("TFT LCD test"));
    pinMode(27, INPUT);
    pinMode(22, OUTPUT);
    Serial.print("TFT size is ");
    Serial.print(tft.width());
    Serial.print("x");
    Serial.println(tft.height());
    tft.reset();
    Serial.print("ID: ");
    Serial.println(tft.readID(), HEX);
    uint16_t identifier = 0x9486;
    tft.begin(identifier);
    tft.fillScreen(TFT_BLACK);
    tft.setRotation(3);
    if (SD.begin(5))
    {
        sd_init = true;
    }
    tft.fillRect(0, 0, 480, 320, TFT_BLACK);
   
}
void loop(void)
{
    while(1)
    {
        //tft.setFont(&FreeSerif24pt7b);
        tft.fillRect(0,50,480,100, TFT_BLACK);
        kb.drawKeys(&tft);
        delay(2000);
    }
}

"Keyboard.h" file

#ifndef _KEYBOARD_H
#define _KEYBOARD_H
 
#include "Adafruit_GFX.h"
#include <MCUFRIEND_kbv.h>
 
class Keyboard {
    private:
 
    public:
        Keyboard(short _h, short _w, short _yel, short _xel, short _bmargins, short _sidepad);
        void drawKeys(MCUFRIEND_kbv* _tft);
};
#endif

"Keyboard.cpp" file

#include "Keyboard.h"
#include "MCUFRIEND_kbv.h"
#include "Adafruit_GFX.h"
 
short xEl = 0, yEl = 0, bMargins = 0, sidePadding = 0, width = 0, height = 0;
unsigned short bBorder = 0, bBackground = 0, bTextColor = 0;
 
const char Mobile_KB[3][13] PROGMEM = {
{0, 13, 10, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
{2, 11, 9, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'},
{2, 11, 7, 'Z', 'X', 'C', 'V', 'B', 'N', 'M'},
};
 
const char Mobile_NumKeys[3][13] PROGMEM = {
{0, 13, 10, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
{2, 11, 9, '-', '/', ':', ';', '(', ')', '&', '@', '"'},
{2, 11, 7, '.', ',', '?', '!', '\'', '`', '$'}
};
 
const char Mobile_SymKeys[3][13] PROGMEM = {
{0, 13, 10, '[', ']', '{', '}', '#', '%', '^', '*', '+', '='},
{2, 11, 9, '_', '\\', '|', '~', '<', '>', '$', '"', '&'}, //4
{2, 11, 7, '.', ',', '?', '!', '\'', '`', '@'}
};
 
Keyboard::Keyboard(short _h, short _w, short _yel, short _xel, short _bmargins, short _sidepad)
{
    height = _h;
    width = _w;
    yEl = _yel;
    xEl = _xel;
    bMargins = _bmargins;
    sidePadding = _sidepad;
}
 
void drawKeys(MCUFRIEND_kbv &_tft)
{
    int yoffset = height - (height * yEl);
    for (int i = 0; i < yEl; i++)
    {
        for (int x = 0; x < xEl; x++)
        {
            _tft.fillRoundRect(((width*x)+bMargins+sidePadding),(((height*i)+yoffset)+bMargins), width-(2*bMargins), height-(2*bMargins), 8, TFT_DARKCYAN);
            _tft.drawRoundRect(((width*x)+bMargins+sidePadding),(((height*i)+yoffset)+bMargins), width-(2*bMargins), height-(2*bMargins), 8, TFT_CYAN);
        }
    }
}

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

50Special
  • 33
  • 6
  • 1
    I recommend you break this _leading underscore habit_ you've adopted. You must not have file-scope variables that start with an underscore. (e.g. `int _width`) [Here's why](https://stackoverflow.com/a/228797/1563833) – Wyck Dec 08 '21 at 14:43
  • 1
    May I also recommend that you replace `#define XELEMENTS 13` with an anonymous const variable. `namespace { const short XELEMENTS = 13; }` Because of [this](https://stackoverflow.com/questions/1637332/static-const-vs-define) and [this](https://stackoverflow.com/questions/4422507/superiority-of-unnamed-namespace-over-static) – Wyck Dec 08 '21 at 14:51
  • Got these too! I will adjust the code following your reccomendations. – 50Special Dec 08 '21 at 15:38

2 Answers2

1

HEADER:

void drawKeys(MCUFRIEND_kbv* _tft);

CPP:

void drawKeys(MCUFRIEND_kbv &_tft)

Those are not equivalent. Decide on one signature and use it.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

There are two things wrong with your definition of drawKeys:

  1. you've got & instead of *
  2. you forgot to qualify the definition with Keyboard::

In the Keyboard.cpp file,

void drawKeys(MCUFRIEND_kbv &_tft)

Should be changed to say

void Keyboard::drawKeys(MCUFRIEND_kbv* _tft)
Wyck
  • 10,311
  • 6
  • 39
  • 60
  • Ok, I've understood the */& switch. I'm new to pointers and got mistaken, my fault. But the Keyboard:: prefix gave me "extra qualification 'Keyboard::' on member 'drawKeys' [-fpermissive]" And the error of undefined reference stay in place :( – 50Special Dec 08 '21 at 15:31
  • In the Keyboard.h i got: `void drawKeys(MCUFRIEND_kbv* myTft);` In the Keyboard.cpp: `void drawKeys(MCUFRIEND_kbv* myTft)` and I call the function as `myTft->functionToRun();` In the main.cpp: `kb.drawKeys(&tft);` Am I doing right? – 50Special Dec 08 '21 at 15:39
  • I think you may have mistakenly attempted to change your header when following my advice. That's not right. The change is only to the .cpp file. The Keyboard.h file should say `void drawKeys(MCUFRIEND_kbv* _tft);` and the keyboard.cpp file should say `void Keyboard::drawKeys(MCUFRIEND_kbv* _tft)`. Please be aware: _declaration_ is in the header file, _definition_ is in the cpp file. I said you have to qualify the ___definition___,, whereas the _declaration_ is already inside braces that associates it with the class so it doesn't need to be further qualified with `Keyboard::`. – Wyck Dec 08 '21 at 16:00
  • That worked! What a noob mistake Thank you very much for all the help and reccomendations! – 50Special Dec 08 '21 at 16:03