N. Trujillo
N. Trujillo

Reputation: 1

Not declared in this scope despite the include of correct file

I am trying to re-write a portion of lazyfoo.net's tutorial on mouse events just for the practice. I have re-thought my design after someone on this site suggested I do. I have decided to try adding a graphics class and also making sure that members of a class to not inherit from the class they are a member of, which was one of the problems the last time I posted a question. Please be patient with me because I am not a very experienced programmer and I am in the middle of learning SDL2.0.

I am running Windows 10 on a 64-bit Dell laptop and using Code::Blocks 16.01.

I tried googling this problem and I don't think any of the questions I found pertain to my specific problem, so after fixing a bunch of other errors I have finally came to an error that I don't know how to fix.

After trying to compile the following code I get an error message that says "'mSpriteClips' was not declared in this scope" despite the fact that I included CentralClass, which is where mSpriteClips lives, into my cpp file.

Here is the code.

lbutton.cpp

#include "lbutton.h"
#include "lButtonSprite.h"
#include "global.h"
#include "graphics.h"
#include "ltexture.h"
#include "central_class.h"

LButton::LButton()
{
    mButtonPosition.x = 0;
    mButtonPosition.y = 0;

    mCurrentSprite = button::MOUSE_OUT;
}

void LButton::setPosition( int x, int y )
{
    mButtonPosition.x = x;
    mButtonPosition.y = y;
}

void LButton::handleEvent(SDL_Event* e)
{
    //If mouse event happened
    if(e->type == SDL_MOUSEMOTION || e->type == SDL_MOUSEBUTTONDOWN || e->type == SDL_MOUSEBUTTONUP)
    {
        //Get mouse position
        int x, y;
        SDL_GetMouseState( &x, &y );

        //Check if mouse is in button
        bool inside = true;

        //Mouse is left of the button
        if(x < mButtonPosition.x)
            inside = false;
        //Mouse is right of the button
        else if(x > mButtonPosition.x + global::BUTTON_WIDTH)
            inside = false;
        //Mouse above the button
        else if(y < mButtonPosition.y)
            inside = false;
        //Mouse below the button
        else if(y > mButtonPosition.y + global::BUTTON_HEIGHT)
            inside = false;

        //Mouse is outside button
        if(!inside)
            mCurrentSprite = button::MOUSE_OUT;
    }
    //Mouse is inside button
    else
    {
        //Set mouse over sprite
        switch(e->type)
        {
        case SDL_MOUSEMOTION:
            mCurrentSprite = button::MOUSE_OVER_MOTION;
            break;

        case SDL_MOUSEBUTTONDOWN:
            mCurrentSprite = button::MOUSE_DOWN;
            break;

        case SDL_MOUSEBUTTONUP:
            mCurrentSprite = button::MOUSE_UP;
            break;
        }
    }
}
void LButton::render(Graphics &graphics)
{
    //Show current button sprite
    mButtonSpriteSheet->render(mButtonPosition.x, mButtonPosition.y, graphics, &mSpriteClips[mCurrentSprite]);
}

and the file that has the member that the compiler says is not declared.

#ifndef CENTRAL_CLASS_H_INCLUDED
#define CENTRAL_CLASS_H_INCLUDED

#include <SDL.h>
#include "lButtonSprite.h"
#include "global.h"
#include "graphics.h"

class LButton;

class CentralClass : public Graphics
{
    // Button objects
    LButton *mButtons[global::TOTAL_BUTTONS];

    // Mouse button sprite rectangles
    SDL_Rect mSpriteClips[button::TOTAL];

public:
    // Call all functions
    CentralClass();

    // Starts up SDL and creates window
    bool init();

    // Loads media
    //bool loadMedia();

    // Main part of the program
    void mainLoop();

    // Frees media and shuts down SDL
    void close();

    // Clean up
    ~CentralClass();
};

#endif // CENTRAL_CLASS_H_INCLUDED

Any help is appreciated. I wish these error messages actually told you what the problem was. That would make coding much more enjoyable.

Upvotes: 0

Views: 1506

Answers (1)

Sam Varshavchik
Sam Varshavchik

Reputation: 118300

I get an error message that says "'mSpriteClips' was not declared in this scope" despite the fact that I included CentralClass, which is where mSpriteClips lives,

No, it does not. That header file does not declare an object called "mSpriteClips". It does declare a class that happens to have a member of that name:

class CentralClass : public Graphics
{
      // ...

    SDL_Rect mSpriteClips[button::TOTAL];

Details matter. mSpriteClips is not a global object, it's a member of a class. Your error message is apparently coming from here:

void LButton::render(Graphics &graphics)
{
    mButtonSpriteSheet->render(mButtonPosition.x, mButtonPosition.y, graphics, &mSpriteClips[mCurrentSprite]);
}

This a method called render() in a class called LButton, which is, obviously, not CentralClass; hence having a member of a class referring to a member of some completely unrelated class is not likely to succeed.

It is not clear what is the intent of this chunk of code. Even if render() can find an instance of the CentralClass class, somewhere, it would not accomplish anything, since mSpriteClips is a private class member, and LButton will not have access to this private class member anyway.

But, this does answer the question of why you are getting your compilation error.

Upvotes: 4

Related Questions