【问题标题】:When declaring a variable: variable not declared in this scope声明变量时:未在此范围内声明的变量
【发布时间】:2015-01-28 19:15:53
【问题描述】:

我有代码:loading* loadingScreen = new loading(device);

当我编译程序时,编译器抱怨没有声明加载屏幕:

error: 'loadingScreen' was not declared in this scope
     loading* loadingScreen = new loading(device);
              ^

我不确定是什么原因造成的。我试过的:

  • 重写它以使构造函数不运行。
  • 将声明重新定位到不同的代码部分。
  • 确保正确包含我的课程。

这是我的 main.cpp 文件:

#include <iostream>
#include <irrlicht.h>
#include <future> // std::async, std::future
#include <chrono> // Millisecond timer
#include <pthread.h>
#include <loading.h>
#include <fps.h>

bool loading = false;

struct load_struct {
    irr::IrrlichtDevice *device;
    fps *fpsLevel;
};

void load(void * loadingStruct)
{
    loading = true;
    struct load_struct *args = (struct load_struct *) loadingStruct;

    loading = false;
    pthread_exit(NULL);
}

int main()
{
    //Create display to optimal settings.
    irr::IrrlichtDevice *device = irr::createDevice(irr::video::EDT_NULL);
    irr::s32 depth = device->getVideoModeList()->getDesktopDepth();
    irr::core::dimension2d<irr::u32> resolution = device->getVideoModeList()->getDesktopResolution();
    device->drop();
    device = irr::createDevice(irr::video::EDT_OPENGL, resolution, depth, true, true, true, NULL); // TODO: Change last parameter
    if(device==NULL)
    {
        std::cout << "Unable to create device! Do you have graphics drivers installed?" << std::endl;
        return 1;
    }

    //Open data files
    device->getFileSystem()->addFileArchive("Resources",true,true,irr::io::EFAT_ZIP);

    device->getFileSystem()->addFileArchive("Video.tar");
    device->getFileSystem()->addFileArchive("Textures.tar");
    device->getFileSystem()->addFileArchive("Models.tar");
    device->getFileSystem()->addFileArchive("Audio.tar");

    //Load first room.
    loading* loadingScreen = new loading(device);
    fps *fpsLevel = new fps();
    pthread_t creationThread;
    struct load_struct loadingStruct;
    loadingStruct.device = device;
    loadingStruct.fpsLevel = fpsLevel;
    //pthread_create(creationThread,NULL,load,(void *)&loadingStruct); Fix me!
    while(loading==false)
    {
        loadingScreen->update();
        std::this_thread::sleep_for(std::chrono::milliseconds(1000/60));
    }

    loadingScreen->kill();

    // Run first room.
    fpsLevel->run();

    //Clean up.
    device->drop();
    return 0;
}

这是我的 loading.h 文件:

#include <irrlicht.h>
#include <string>
#ifndef LOADING_H
#define LOADING_H

class loading
{
public:
    loading(irr::IrrlichtDevice *device);
    void update();
    void kill();
protected:
    int slide;
    static const int SLIDES_AMOUNT = 60;
    const std::string FILENAME_TEMPLATE = "Loading_Screen/%.png";
    irr::video::ITexture* slides[SLIDES_AMOUNT];
};

#endif // LOADING_H

任何帮助将不胜感激!

【问题讨论】:

  • 你能提供更多关于有问题的代码行在哪里的上下文吗?仅凭您在此处提供的信息,我认为没有足够的信息可以提供帮助。
  • 我认为您提到的错误是 second 错误,它是由 first 错误引起的误报。即loading没有new loading()调用的默认构造函数。
  • 当你去的时候会发生什么 ==> loading* loadingScreen = new loading;
  • 我有点想我会在这里被否决......但我真的被困住了,我在其他任何地方都没有看到这个问题。这是编译器提到的第一个错误,所以它不能是误报。 new loading() 实际上应该是 new loading(device);这是我的一个错字,将在上面更正。我还将附上我的主要课程。
  • 您有一个bool loading 和一个class loading。重命名 main.cpp 顶部的 bool 并想知道。

标签: c++ class variable-declaration


【解决方案1】:

问题可能是由于您有两个不同的东西,称为loading

首先,这是您的class loading。但是,还有变量bool loading。所以当你这样做时:

loading* loadingScreen = new loading(device);

编译器面临歧义,似乎更喜欢变量 loading 而不是类型 loading(我不是 C++ 专业人士,无法解释为什么会这样;我的猜测是因为变量是该名称的最新定义)。

解决方法很简单:使两个名称不同。由于 C++ 区分大小写,因此您应该采用以大写字母开头的类名称的约定:将其设为 class LoadingLoading* loadingScreen = new Loading(device);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-18
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多