Variable scope

Hi so i have a scope question...i feel like its a common thing but couldn't find anything on the forums. if it already exists could someone please point me to it.

So the the project im busy with spans across the ino file as well as a couple of header and cpp files. the structure in question is as follows.

i have a class called slider initialized in slider.h:

class Slider
{
public:
    boolean isDefaultSequenceSet;
    Sequence defaultSequence;
    void calibrate();
};

its instantiated in the main .ino file called CameraSlider.ino:

#include "UI.h"
#include "Slider.h"
#include "Motor.h"

Slider slider;
Motor motorX(A8, 48, 46);
UI ui;
void setup()
{
    motorX.init();
    motorX.setStepSize(16);
    ui.setView(SPLASH);
    //calibrate();
}

void loop()
{

}

now all the pieces of code in the UI.cpp file that try to access the instance of slider on the main ino file gives an error:

Compiling 'CameraSlider' for 'Arduino Mega w/ ATmega2560 (Mega 2560)' UI.cpp:In member function 'void UI::setView(View)' UI.cpp:9:7: error: 'slider' was not declared in this scope UI.cpp:In member function 'void UI::splashView()' UI.cpp:43:2: error: 'slider' was not declared in this scope Error compiling

#include "UI.h"
void UI::setView(View view)
{
    this->currentView = view;
    switch (view)
    {
    case SPLASH:
        splashView();
        if (slider.isDefaultSequenceSet == true)
            sequencePlayerView(slider.defaultSequence);
        else
            savedSequencesView();
        break;

    case SEQUENCEPLAYER:
            /*sequencePlayerView();*/
        break;

    case SAVEDSEQUENCES:
        savedSequencesView();
        break;

    case SEQUENCEEDITOR:
        sequenceEditorView();
        break;

    case MOTIONEDITOR:
        motionEditorView();
        break;

    case MANUALCONTROLLER:
        manualControlView();
        break;

    case SETTINGS:
        settingsView();
        break;
    }
}

void UI::splashView()
{
    slider.calibrate();
}

void UI::sequencePlayerView(Sequence sequence)
{

}

note that i am using visual micro so the errors may look a bit different to the arduino ide but that also kicks up a fuss.

any idea whats up??

perhaps alot of figuring out if there is a problem with my code can be avoided if i say that if i initialize a int called test in place of the slider instanciation i also cannot access it in my external UI.cpp file.

i have a class called slider initialized in slider.h:

No, you don't.

now all the pieces of code in the UI.cpp file that try to access the instance of slider on the main ino file gives an error:

How is the compiler supposed to know, when compiling the UI class' files, that some other file contains a definition of that variable?

any idea whats up??

Yes. You are making assumptions that are not valid. Nowhere do you tell the UI class that there is something called slider that it needs to know about.

okay so how would i tell the UI class that there is an instance of slider in the main ino file that i want it to use? sorry for my lacking coding knoledge

okay so how would i tell the UI class that there is an instance of slider in the main ino file that i want it to use?

Pass the instance to the constructor or add a method that takes a Slider instance.

Most likely, though, you'll want to use references or pointers, rather than instances.

Okay! Thank you so much for the direction! I will look into the techniques you mentioned thanks paulS

t3rror23: Okay! Thank you so much for the direction! I will look into the techniques you mentioned thanks paulS

I'd show some possibilities, but you didn't post the UI.h file...

that will be greatly appreciated! this is the UI.h file:)

// UI.h

#ifndef _UI_h
#define _UI_h

//#if defined(ARDUINO) && ARDUINO >= 100
    #include "arduino.h"
    #include "Slider.h"
//#else
//  #include "WProgram.h"
//#endif

enum View
{
    SPLASH,
    SEQUENCEPLAYER,
    SAVEDSEQUENCES,
    SEQUENCEEDITOR,
    MOTIONEDITOR,
    MANUALCONTROLLER,
    SETTINGS
};

class UI
{
private:
    View currentView;
    void splashView();
    void sequencePlayerView(Sequence sequence);
    void savedSequencesView();
    void sequenceEditorView();
    void motionEditorView();
    void manualControlView();
    void settingsView();

public:
    void setView(View view);
};

#endif

All classes should have constructors and destructors, even if they do nothing.

You could have a constructor that takes a Slider reference:

   UI(Slider &theSliderToOperateOn);

Then, you need to store that so that other methods can use it:

private:
   Slider &mSlider;

Now, implement that:

UI::UI(Slider &sliderIn)
{
   mSlider = sliderIn;
}

Or, you could have the same member field, and a new method

   void setSlider(Slider &slipSlideAway);

void UI::setSlider(Slider &slippery)
{
   mSlider = slippery;
}

In the sketch, use either

UI ui(slider);

or

UI ui;

and

    ui.setSlider(slider);

In the UI class, then, use mSlider instead of slider.

You can do similar things with pointers, but then you'd need to use mSlider->Xxx everywhere, instead of mSlider.Xxx.