Skip to main content

I'm having some programming problems.

So here's what's happening:

inIn my .ino file, iI have a "tft"tft variable which holds the connection to a lcdLCD. I want to make a class which can either automatically access that "tft"tft class contained in the .ino, or pass it into the constructor when it's created; so basically:

// assuming this is the .ino:

TFT_ILI9163C tft = TFT_ILI9163C(cs, dc, rst);


MenuClass *menu = MenuClass();
menu->init("Example", tft); // this should work and "menu" should now have access to the "tft" variable

Here's my Menu.cpp:

// Menu class

#include "Menu.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>

String menName = "Unnamed";

void MenuClass::init(const String name) 
{
    menName = name;
}

String MenuClass::getName() {
    return menName;
}

void MenuClass::draw() {
    tft.setCursor(2, 2);
    tft.print(menName);
}

MenuClass Menu;

I'm having some programming problems.

So here's what's happening:

in my .ino file, i have a "tft" variable which holds the connection to a lcd. I want to make a class which can either automatically access that "tft" class contained in the .ino, or pass it into the constructor when it's created; so basically:

// assuming this is the .ino:

TFT_ILI9163C tft = TFT_ILI9163C(cs, dc, rst);


MenuClass *menu = MenuClass();
menu->init("Example", tft); // this should work and "menu" should now have access to the "tft" variable

Here's my Menu.cpp:

// Menu class

#include "Menu.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>

String menName = "Unnamed";

void MenuClass::init(const String name) 
{
    menName = name;
}

String MenuClass::getName() {
    return menName;
}

void MenuClass::draw() {
    tft.setCursor(2, 2);
    tft.print(menName);
}

MenuClass Menu;

I'm having some programming problems.

So here's what's happening:

In my .ino file, I have a tft variable which holds the connection to a LCD. I want to make a class which can either automatically access that tft class contained in the .ino, or pass it into the constructor when it's created; so basically:

// assuming this is the .ino:

TFT_ILI9163C tft = TFT_ILI9163C(cs, dc, rst);


MenuClass *menu = MenuClass();
menu->init("Example", tft); // this should work and "menu" should now have access to the "tft" variable

Here's my Menu.cpp:

// Menu class

#include "Menu.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>

String menName = "Unnamed";

void MenuClass::init(const String name) 
{
    menName = name;
}

String MenuClass::getName() {
    return menName;
}

void MenuClass::draw() {
    tft.setCursor(2, 2);
    tft.print(menName);
}

MenuClass Menu;
Tweeted twitter.com/StackArduino/status/683627770937786369
Source Link
vxcheese
  • 51
  • 1
  • 2

Basic C++ programming, how to pass constructor argument into class?

I'm having some programming problems.

So here's what's happening:

in my .ino file, i have a "tft" variable which holds the connection to a lcd. I want to make a class which can either automatically access that "tft" class contained in the .ino, or pass it into the constructor when it's created; so basically:

// assuming this is the .ino:

TFT_ILI9163C tft = TFT_ILI9163C(cs, dc, rst);


MenuClass *menu = MenuClass();
menu->init("Example", tft); // this should work and "menu" should now have access to the "tft" variable

Here's my Menu.cpp:

// Menu class

#include "Menu.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <TFT_ILI9163C.h>

String menName = "Unnamed";

void MenuClass::init(const String name) 
{
    menName = name;
}

String MenuClass::getName() {
    return menName;
}

void MenuClass::draw() {
    tft.setCursor(2, 2);
    tft.print(menName);
}

MenuClass Menu;