0

VS2019 says that GLFWwindow* is not initialized. It says it as a warning and then does not compile. Also it worked before and it says it only in the start_window function.

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

I tried: (Advice from BDL) To initialize it with nullptr - invisible and unresponsive window. (VS IntelliSense) To do window{}. Same result.

I have "Treat warnings as errors" off.

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 (probably not needed for this question but im going to include it anyway)

#pragma once

#include <iostream>

#include <GLFW/glfw3.h>

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

Any clue?

PS. Do anyone know why?

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • [C4700](https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-1-and-level-4-c4700?view=vs-2019) is usually a warning. Do you have "Treat warnings as errors" enabled? But why not initialize it? `GLFWwindow* window = nullptr;`? – BDL Jun 26 '20 at 14:53
  • makes the window invisible and not responding – MegapolisPlayer Jun 26 '20 at 14:56
  • 3
    Why are you passing `window` by value into `start_window()` instead of by reference? – genpfault Jun 26 '20 at 15:02
  • to instead of writing 3lines, write 1 (simplifying purposes) – MegapolisPlayer Jun 26 '20 at 15:05
  • The warning is correct. The variable is uninitialized. Trying to use the thing it points to will probably crash your program. – user253751 Jun 26 '20 at 18:11

1 Answers1

3

C4700 is usually a warning and only behaves like an error when "Treat warnings as errors" is enabled

To get rid of this warning, you should initialize the variable with:

GLFWwindow* window = nullptr;

makes the window invisible and not responding

This happens because the window parameter of start_window does not do what you want. You want to set the value of a pointer from inside the method, but for this you have to pass a reference to a pointer (or a pointer to a pointer). Your current code passes the pointer by value but does not return the new window point to the calling method. You can use something like the next code sample to fix the problem:

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;
    }
}
BDL
  • 21,052
  • 22
  • 49
  • 55
  • 1
    @Rabbid76: Would also work. Probably you are right and it's the cleaner solution. I'm going to edit. – BDL Jun 26 '20 at 15:07