类中的枚举在调用自己的函数时不会改变

enum in class doesn't change when call own function

本文关键字:函数 改变 自己的 调用 枚举      更新时间:2023-10-16

简短的问题描述

我有一个问题,当我更改枚举时,它没有改变。
它看起来只是一个局部变化。

如果我做下面的报价就可以了。

level = new level_1();
while(true)
{
    cout << level->state << endl; // show '1' at first loop. Another loop show '2'
    level->state = STATE_INITIALIZE;
}

但我必须改变他函数的状态

level = new level_1();
while(true)
{
    cout << level->state << endl; // show '1' only
    level.do();
}

level.do 功能

void Level_1::do()
{
    state = STATE_INITIALIZE; // If this work, it should be '2'
    // this->state = STATE_INITIALIZE;
}

所以这对我来说是一个大问题,它不会改变!!T_T


长代码

这是一些代码(几乎是完整的代码(

水平.h

enum LEVEL_STATE
{       STATE_LOAD = 1,
        STATE_INITIALIZE = 2,
        STATE_UPDATE = 3,
        STATE_DRAW = 4,
        STATE_FREE = 5,
        STATE_UNLOAD = 6
};
class Level {
public:
    LEVEL_STATE state = STATE_LOAD;
    vector<Shader> shader;
    vector<Model> model;
    virtual void load()=0;
    virtual void initialize()=0;
    virtual void update()=0;
    virtual void draw(Shader)=0;
    virtual void free()=0;
    virtual void unload()=0;
    virtual Level* next()=0;
};

Map_Level_1.h

#include"Level.h"
class Level_1 : public Level {
public:
    LEVEL_STATE state;
    vector<Shader> shader;
    vector<Model> model;
    void load();
    void initialize();
    void update();
    void draw(Shader);
    void free();
    void unload();
    Level* next();
};

Map_Level_1.cpp

#pragma once
#include"Map_Level_0.h"
#include"Map_Level_1.h"
extern Camera camera;
extern float SCR_WIDTH;
extern float SCR_HEIGHT;
void Level_1::load()
{
    shader.push_back(Shader("../Shader/alpha.vertex", "../Shader/alpha.fragment")); // this worked!
    model.push_back(Model (FileSystem::getPath("res/arcade/arcade.obj"))); // this worked!
    state = STATE_INITIALIZE; // this is not worked! the value doesn't change!
}
void Level_1::initialize()
{
    shader[0].use();
    state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::update()
{
    shader[0].use();
    state = STATE_DRAW; // this is not worked! the value doesn't change!
}
void Level_1::draw(Shader shader)
{
    glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 10000.0f);
    glm::mat4 view = camera.GetViewMatrix();
    glm::mat4 model;
    shader.setMat4("projection", projection);
    shader.setMat4("view", view);
    shader.setInt("mode", 0);
    //model = scale(vec3(5.0f, 5.0f, 5.0f));
    shader.setMat4("model", model);
    shader.setInt("fragMode", 1);
    shader.setVec3("fragColor", vec3(0.92f, 0.92f, 0.92f));
    this->model[0].Draw(shader);
    state = STATE_UPDATE; // this is not worked! the value doesn't change!
}
void Level_1::free()
{
}
void Level_1::unload()
{
}
Level* Level_1::next() {
    return new Level_0();
}

主.cpp

level = new Level_1();
while (!glfwWindowShouldClose(window))
{
    DEV_processInput(window);
    cout << level->state;
    switch (level->state)
    {
    case STATE_LOAD:
        level->load();
    case STATE_INITIALIZE:
        level->initialize();
    case STATE_UPDATE:
        level->update();
    case STATE_DRAW:
        level->draw(shader_1);
    case STATE_FREE:
        level->free();
    case STATE_UNLOAD:
        level->unload();
    }
    glfwSwapBuffers(window);
    glfwPollEvents();
}


如果我1.(做一些容易解决
的错误2.(重复任何问题

我会道歉的..

您在基类中定义了state成员,然后在子类中重新定义了同一成员,这导致了问题。

如果你有一个Level * level指针,无论它指向哪个子类实例,你总是读取基类state成员,同时你用他们的方法写入子类成员(即 do (。

我建议仅在基类中定义state(并使其protected,也许(。