提问者:小点点

C++VISUAL Studio:GLFWwindow未初始化,即使它是在文档中编写的并且以前工作过


VS2019表示GLFWWindow*未初始化。 它说它作为警告,然后不编译。 而且它以前也工作过,它只在start_window函数中说它。

...main.cpp(19): error C4700: uninitialized local variable 'window' used
...Done building project "CityEngine.vcxproj" -- FAILED.

我尝试了:(来自BDL的建议)用nullptr初始化它-不可见和无响应的窗口。 (VS IntelliSense)执行窗口{}。 同样的结果。

main.cpp

#include "city/stdheader.h"
#include "city/city.h"
//Target board :)
//put colors in front of stuff (thanks stack overflow)
float color1[3] = { 1.0, 1.0, 1.0 };
float color2[3] = { 1.0, 0.0, 0.0 };
float colorA[3] = { 0.3, 0.3, 0.3 };
float vertices1[8] = { 0.9, 0.9, -0.9, 0.9, -0.9, -0.9, 0.9, -0.9 };
float vertices2[8] = { 0.7, 0.7, -0.7, 0.7, -0.7, -0.7, 0.7, -0.7 };
float vertices3[8] = { 0.5, 0.5, -0.5, 0.5, -0.5, -0.5, 0.5, -0.5 };
float vertices4[8] = { 0.3, 0.3, -0.3, 0.3, -0.3, -0.3, 0.3, -0.3 };
float vertices5[8] = { 0.1, 0.1, -0.1, 0.1, -0.1, -0.1, 0.1, -0.1 };
float vertices1A[6] = { 0, 0, -0.2, 0.2, -0.2, -0.1 };
float vertices2A[6] = { -0.4, 0.3, -0.6, 0.5, -0.6, 0.2 };
float vertices3A[6] = { 0.4, -0.6, 0.2, -0.4, 0.2, -0.7 };
int main() {
    glfwInit();
    GLFWwindow* window;
    City::start_window(window, 680, 480);
    while (!CtWINDOW_OPEN(window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(1.0, 1.0, 1.0, 1.0);
        City::drawpoly(vertices1, color2);
        City::drawpoly(vertices2, color1);
        City::drawpoly(vertices3, color2);
        City::drawpoly(vertices4, color1);
        City::drawpoly(vertices5, color2);
        //Arrows
        City::drawtriangle(vertices1A, colorA);
        City::drawtriangle(vertices2A, colorA);
        City::drawtriangle(vertices3A, colorA);
        CtPROCESS(window);
    }
    glfwTerminate();
}

CityEngine.cpp

#include "stdheader.h"
#include "city.h"
#include <GLFW/glfw3.h>

namespace City {
    void terminate_window() {
        glfwTerminate();
    }
    void start_window(GLFWwindow* window, float width, float height) {
        glfwInit();
        window = glfwCreateWindow(width, height, "CITYEngine", 0, 0);
        glfwMakeContextCurrent(window);
        if (!window)
        {
            terminate_window();
        }

        //while (!WINDOW_OPEN(CITYwindow)) {
        //    PROCESS(CITYwindow);
       // }
    }
    void drawpoly(float vertices[8], float color[3]) {
        glBegin(GL_POLYGON);
        glColor3f(color[0], color[1], color[2]);
        glVertex2f(vertices[0], vertices[1]);
        glVertex2f(vertices[2], vertices[3]);
        glVertex2f(vertices[4], vertices[5]);
        glVertex2f(vertices[6], vertices[7]);
        glEnd();
    }
    void drawtriangle(float vertices[6], float color[3]) {
        glBegin(GL_TRIANGLES);
        glColor3f(color[0], color[1], color[2]);
        glVertex2f(vertices[0], vertices[1]);
        glVertex2f(vertices[2], vertices[3]);
        glVertex2f(vertices[4], vertices[5]);
        glEnd();
    }
}

城市h

#pragma once

#define CtWINDOW_OPEN(window) glfwWindowShouldClose(window)
#define CtPROCESS(window) glfwSwapBuffers(window); glfwPollEvents()

namespace City {
    void start_window(GLFWwindow* window, float width, float height);
    void terminate_window();
    void drawpoly(float vertices[8], float color[3]);
    void drawtriangle(float vertices[6], float color[3]);
}

stdheader(这个问题可能不需要,但我无论如何都要包含它)

#pragma once

#include <iostream>

#include <GLFW/glfw3.h>

using std::string;
using std::cout;
using std::cin;

有线索吗?

有人知道为什么吗?


共1个答案

匿名用户

C4700通常是一个警告,只有在启用“将警告视为错误”时才会像错误一样表现

若要消除此警告,应使用以下命令初始化变量:

GLFWwindow* window = nullptr;

使窗口不可见且不响应

发生这种情况是因为start_window的窗口参数没有执行您想要的操作。 您希望从方法内部设置指针的值,但为此您必须传递对指针的引用(或指向指针的指针)。 您的当前代码按值传递指针,但不将新窗口点返回给调用方法。 您可以使用类似于下一个代码示例的内容来修复该问题:

void start_window(GLFWwindow*& window, float width, float height)
{
    glfwInit();
    auto createdWindow = glfwCreateWindow(width, height, "CITYEngine", 0, 0);
    glfwMakeContextCurrent(createdWindow );
    if (!createdWindow )
    {
        terminate_window();
        window = nullptr;
    }
    else
    {
        window = createdWindow;
    }
}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(c++visual|studio|glfwwindow|未|初始化|文档|中|编写|工|作过)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?