C++ VISUAL STUDIO: GLFWwindow 未初始化,即使它写在文档中并且之前工作过
C++ VISUAL STUDIO: GLFWwindow not initialized even if it is written in the docs and worked before
VS2019 说 GLFWwindow* 没有初始化。它说这是一个警告,然后不编译。
它之前也有效,它只在 start_window 函数中说。
...main.cpp(19): error C4700: uninitialized local variable 'window' used
...Done building project "CityEngine.vcxproj" -- FAILED.
我试过:
(来自 BDL 的建议)用 nullptr 初始化它 - 不可见且无响应 window。
(VS IntelliSense)做 window{}。相同的结果。
我关闭了“将警告视为错误”。
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();
}
}
city.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;
有线索吗?
PS。有谁知道为什么?
C4700 is usually a warning and only behaves like an error when "Treat warnings as errors" is enabled
要消除此警告,您应该使用以下方法初始化变量:
GLFWwindow* window = nullptr;
makes the window invisible and not responding
发生这种情况是因为 start_window
的 window 参数没有按照您的要求执行。您想从方法内部设置指针的值,但为此您必须传递对指针的引用(或指向指针的指针)。您当前的代码按值传递指针,但不 return 新的 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;
}
}
VS2019 说 GLFWwindow* 没有初始化。它说这是一个警告,然后不编译。 它之前也有效,它只在 start_window 函数中说。
...main.cpp(19): error C4700: uninitialized local variable 'window' used
...Done building project "CityEngine.vcxproj" -- FAILED.
我试过: (来自 BDL 的建议)用 nullptr 初始化它 - 不可见且无响应 window。 (VS IntelliSense)做 window{}。相同的结果。
我关闭了“将警告视为错误”。
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();
}
}
city.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;
有线索吗?
PS。有谁知道为什么?
C4700 is usually a warning and only behaves like an error when "Treat warnings as errors" is enabled
要消除此警告,您应该使用以下方法初始化变量:
GLFWwindow* window = nullptr;
makes the window invisible and not responding
发生这种情况是因为 start_window
的 window 参数没有按照您的要求执行。您想从方法内部设置指针的值,但为此您必须传递对指针的引用(或指向指针的指针)。您当前的代码按值传递指针,但不 return 新的 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;
}
}