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 初始化它 - 不可见且无响应的窗口。 (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;
}
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何在文档文件夹中创建目录
- 当Microsoft文档仅包含 C# 示例时,如何查找 C++ 包含文件名
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 关于类的 Python 文档 - 对C++的引用不正确
- C++/autoconf 等效于文档库要求.txt的要求是什么?
- mbed:使用 USB 文档库编译会导致错误
- C++ VISUAL STUDIO:GLFWwindow没有初始化,即使它是在文档中编写的并且以前工作过
- 使用 yaml-cpp 更新 YAML 文档的节点和值
- 在加载 MSHTML 文档之前从 MSHTML 文档中删除无效的 URL
- 如何在C++中表示JSON文档的递归对象结构?
- 如何查找 Redshift Maya API 文档?
- 如何使用值模板编写文档测试用例?
- 如何使用 Gradle DSL(领域特定语言)上的文档?
- ncurses 找不到任何关于cchar_t的文档
- 从Windows上的系统工作的服务中获取用户文档文件夹.C++
- 我怎样才能使野牛文档的 calc++ 示例工作
- 如何将数据文件加载到自动Excel工作表文档
- v8javascript API文档在c中工作
- rapidjson:用于从文件中读取文档的工作代码