Using object in a another object

Hi guys!

I have little problem with using one object inside other object.
So, I'm writing code for STM32 and I decided to go with OOP(my First try). Now, I created .h and .cpp files for objects, classes etc..
Few "custom made" objects have global scope(defined at the top in main file) only one object has scope only in setup() function.

But, in code I use objects for display, RTC, servo etc.. (libraries from Arduino).
When I tried to use screen object inside boot object compiler said it's not defined. I googled and found solution that doesn't work( :( )

Error:
1
2
3
C:/Users/silvi/Documents/Arduino/libraries/Adafruit_SH1106_STM32-master/Adafruit_SH1106_STM32.h:132:3: note: Adafruit_SH1106::Adafruit_SH1106(int8_t)
   Adafruit_SH1106(int8_t RST);
C:/Users/silvi/Documents/Arduino/libraries/Adafruit_SH1106_STM32-master/Adafruit_SH1106_STM32.h:132:3: note:   candidate expects 1 argument, 0 provided



Boot.h file:
1
2
3
4
5
6
7
8
9
10
class BootClass {

public:
	BootClass(const SystemClass&, const Adafruit_SH1106&);
	~BootClass();
	

private:
	SystemClass System;
	Adafruit_SH1106 Screen;



Boot.cpp file:
1
2
3
4
5
BootClass::BootClass(const SystemClass &sysObject, const Adafruit_SH1106 &scrObject)
{
    System = sysObject;
    Screen = scrObject;
}



Main file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//----- AT THE TOP
// OBJECTS
Adafruit_SH1106 Screen(4);
RTC_DS3231 RTC;
Servo HLServo;
HardwareTimer timer(LED_PWM_CH);


// MODULES
#include 											"Headers\Settings.h"

#include 											"Headers\System.h"
#include 											"Headers\Boot.h"
#include 											"Headers\UI.h"

#include 											"Headers\EEPROM.cpp"
#include 											"Headers\Accelerometer.cpp"


// CUSTOM OBJECTS
SystemClass System;
UIClass UI;


With System object everything Works fine, why it doesn't work with screen object?

Also I tried with and it doesn't work

1
2
3
4
void BootClass::screenDim() const 
{
	::Screen.dim(System.isDark());
}
Your Adafruit_SH1106 object appears to have one constructor, of form:

Adafruit_SH1106::Adafruit_SH1106(int8_t)

This means there is no other way to create an Adafruit_SH1106 object. They can only be created with that construtor.

Your BootClass object contains an Adafruit_SH1106 object. Your BootClass object constructor must create the Adafruit_SH1106 using the right Adafruit_SH1106 constructor. You must tell it how.

1
2
3
4
BootClass::BootClass(const SystemClass &sysObject, const Adafruit_SH1106 &scrObject) : Screen (scrObject)
{
    System = sysObject;
}


https://stackoverflow.com/questions/7761676/calling-constructor-of-a-class-member-in-constructor

I did like you said but now I get error.

error: class 'BootClass' does not have any field named 'Screen'

If I uncomment "Adafruit_SH1106 Screen;" in header file then I get

error: passing 'const Adafruit_SH1106' as 'this' argument of 'virtual void Adafruit_GFX::drawRect(int16_t, int16_t, int16_t, int16_t, uint16_t)' discards qualifiers [-fpermissive]
Last edited on
Don't pass the Adafruit_SH1106 as a const.
It Works. Thanks
Topic archived. No new replies allowed.