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

C++ VISUAL STUDIO: GLFWwindow not initialized even if it is written in the docs and worked before

本文关键字:文档 工作 GLFWwindow STUDIO VISUAL 初始化 C++      更新时间:2023-10-16

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

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

我试过了: (来自BDL的建议(使用 nullptr 初始化它 - 不可见且无响应的窗口。 (VS 智能感知(待办事项窗口{}。相同的结果。

我关闭了"将警告视为错误"。

主.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();
}

城市引擎.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;

有什么线索吗?

附言。有谁知道为什么?

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

要摆脱此警告,您应该使用以下命令初始化变量:

GLFWwindow* window = nullptr;

使窗口不可见且无响应

发生这种情况是因为start_window的 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;
}
}