gpt4 book ai didi

c++ - 声明变量时 : variable not declared in this scope

转载 作者:行者123 更新时间:2023-11-28 02:33:50 25 4
gpt4 key购买 nike

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

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

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

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

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

这是我的 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

如有任何帮助,我们将不胜感激!

最佳答案

这个问题可能是因为你有两个不同的loading:

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

loading* loadingScreen = new loading(device);

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

修复非常简单:让两个名称不同。由于 C++ 区分大小写,因此您应该采用以大写字母开头类名的约定:使 class LoadingLoading* loadingScreen = new Loading(device);

关于c++ - 声明变量时 : variable not declared in this scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28200540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com