尽管包含正确的文件,但在此范围内未声明

Not declared in this scope despite the include of correct file

本文关键字:范围内 未声明 文件 包含正      更新时间:2023-10-16

我试图重写lazyfoo.net的部分鼠标事件教程,只是为了练习。在这个网站上有人建议我这样做之后,我重新考虑了我的设计。我决定尝试添加图形课程,并确保班级成员不从他们的班级中继承而来,这是我上次发布问题的问题之一。请耐心等待我,因为我不是一个经验丰富的程序员,并且我正在学习SDL2.0。

我正在64位戴尔笔记本电脑上运行Windows 10,并使用Code :: Blocks 16.01。

我尝试谷歌搜索这个问题,但我认为我发现的任何问题都与我的特定问题有关,因此在解决了许多其他错误之后,我终于遇到了一个错误,我不知道如何解决问题。

尝试编译以下代码后,我收到了一条错误消息,该错误消息说" MSPriteClips'在此范围中并未声明",尽管我将MSPriteClips寿命的CentralClass包括在我的CPP文件中。

这是代码。

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]);
}

和具有编译器所说的成员的文件未声明。

#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

任何帮助将不胜感激。我希望这些错误消息实际上告诉您问题是什么。这将使编码更加愉快。

我收到一条错误消息,上面写着"'mspriteclips'未在 这个范围"尽管我包括了CentralClass, MSPriteClips居住的地方,

不,没有。该标头文件不会声明一个称为" mspriteclips"的对象。它确实声明了一个恰好具有该名称成员的课程:

class CentralClass : public Graphics
{
      // ...
    SDL_Rect mSpriteClips[button::TOTAL];

详细信息很重要。mSpriteClips不是全局对象,它是类的成员。您的错误消息显然来自这里:

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

这是一种称为LButton的类中的render()的方法,显然不是CentralClass;因此,有一个班级的成员指的是某些完全不相关的班级的成员不太可能成功。

目前尚不清楚这些代码块的意图是什么。即使render()可以找到CentralClass类的实例,也不会完成任何事情,因为mSpriteClips是私人类成员,并且LButton无论如何都无法访问此私人类成员。

但是,这确实回答了您为什么会遇到汇编错误的问题。