cancel
Showing results for 
Search instead for 
Did you mean: 

c++ object not available outside main.cpp file

Inverse Inductor
Associate
Posted on April 16, 2018 at 01:39

I've got a simple c++ class (LEDDriver) that has an internal state that will eventually be updated by a state machine. I've created an update() member function that will handle turning the LEDs off and on, it just needs to be called in an interrupt.

In my main function, I initialize LED as a LEDDriver object with a global scope, but am unable to call this object from within my stm32l0xx_it.cpp even with main.h and LEDDriver.h included.

Does anyone know why this would not work?

I've attached both the whole project as well as just the code pertaining to the question.

#interrupts #c++
1 REPLY 1
valentin
Senior
Posted on April 16, 2018 at 06:18

Hey,

my suggestion: have a look around for variable scope and how to declare and use global variables. Look for the keyword extern and how to use it.

You're declaring the object in your main.cpp, so only code below that declaration knows about it (inside main.cpp).

If you're putting it into the header file, you could end up having multiple objects - one per translation unit.

You need to do an extern declaration in your header file and ONLY ONE initialization in one .cpp file (e.g. main.cpp).

Stackoverflow should be a good first point of contact with regards to extern.