Hi guys,
I’m trying to figure out how to instantiate a class inside a library I intend to write while passing parameters to the instantiated class in question from within the “wrapper class”. I.e. my new library creates a wrapper class that should give a birth (=instantiate) a class from a sub-library. The constructor of this newly created object takes parameter – and I want to fill in this parameter with a value passed into the wrapper class/object upon its creation.
The ultimate goal is to write my own library which takes care of displaying a menu-like structure on a very standard 16x2 LCD display. Hence for handling the LCD stuff I will be using LiquidCrystal library.
I need to instantiate the LCD object inside my “LCDMenu” library. In order to make my LCDMenu library versatile I obviously want to avoid “harcdoding” LCD pins into my library – I want to pass on LCD pins to the object/class I create inside my LCDMenu library which – in turns – shall instantiate the LCD object with correct pin values (it received upon its creation).
In order to understand the way I created the following test sketch with two simple libraries: class defined in LibA does nothing else then incrementing internal count by 1 upon calling IncrementArg1() function. The initial value is passed into it within class constructor.
LibB does the same plus tries to instantiate a class from LibA. A private pointer to the type of LibA class is defined in the LibA class private section – its name is _nestedlibAprivate. The LibA class is instantiated in the class LibB begin function and is declared as static to survive the begin function termination. So far so good – it compiles well.
However an attempt to pass on the pointer to the newly instantiated class into the private variable fails (currenlty commented out in the code) – an error of “undefined reference to `LibB::_nestedlibAprivate'” is raised during compilation attempt. This step is however essential to be able to work with the LibA object further in other LibB functions.
Any idea why? (And I must admit part of my inability to decipher the issue on my own is derived from the fact I don’t have clear understanding on the use/need of the begin() function).
#include "LibA.h"
#include "LibB.h"
LibA myliba(11);
LibB mylibb(33);
void setup() {
Serial.begin(115200);
mylibb.begin(66);
}
void loop() {
myliba.IncrementArg1();
Serial.print("LibA value: ");
Serial.println(myliba.WhatisArg1());
mylibb.IncrementArg1();
Serial.print("LibB value: ");
Serial.println(mylibb.WhatisArg1());
delay(5000);
}
#ifndef LibA_h
#define LibA_h
#include "Arduino.h"
class LibA
{
public:
LibA(int LibAarg1);
void IncrementArg1();
int WhatisArg1();
private:
int _Ainternal;
};
#endif
#include "Arduino.h"
#include "LibA.h"
LibA::LibA(int LibAarg1)
{
_Ainternal = LibAarg1;
}
void LibA::IncrementArg1()
{
_Ainternal++;
}
int LibA::WhatisArg1() {
return _Ainternal;
}
#ifndef LibB_h
#define LibB_h
#include "Arduino.h"
#include "LibA.h"
class LibB
{
public:
LibB(int LibBarg1);
void begin(int seedint);
void IncrementArg1();
int WhatisArg1();
private:
int _Binternal;
static LibA* _nestedlibAprivate;
};
#endif
#include "Arduino.h"
#include "LibB.h"
#include "LibA.h"
LibB::LibB(int LibBarg1)
{
_Binternal = LibBarg1;
}
void LibB::IncrementArg1()
{
_Binternal=_Binternal+1;
Serial.print("LibB internal increment: ");
Serial.println(_Binternal);
// _nestedlibAprivate->IncrementArg1();
// Serial.print("Nested LibA IncrementArg1: ");
// Serial.println(_nestedlibAprivate->WhatisArg1());
}
int LibB::WhatisArg1() {
return _Binternal;
}
void LibB::begin(int seedint)
{
static LibA s_nestedlibA(seedint);
// _nestedlibAprivate = &s_nestedlibA;
}