Alex V-P
Alex V-P

Reputation: 49

C++ Namespace is undeclared

I'm new to C++ and am trying to learn OpenGL. However, when I try to compile my program, I cannot refer to a namespace that I have already created in my header file. The code is as follows:

engine.hpp:

#ifndef ENGINE_HPP
#define ENGINE_HPP
// ...
namespace render {
  void Initialise();
  namespace inits {
    bool glfw_init = false,
    glfw_window_init = false,
    gl_init = false;
  }
}
#endif

engine.cpp:

#include "engine.hpp"
// ...
namespace render {
  void Initialise() {
    if (glfwInit() == GLFW_FALSE)
    inits::glfw_init = false;
  }
}

O I'm not sure why this isn't compiling. I'm using MinGW (GCC) on Windows 10 (64-bit).

My compiler message is:

error: 'inits' has not been declared

Edit: Thank you everybody for your suggestions. It appears that although this was a compiler fluke (after my first edit, since others could not reproduce the issue and a reinstall fixed the issue), it is a symptom of a larger organisational problem within my code. Since I have only invested ~15 minutes into the program, I would prefer to completely rewrite this after learning more about the language. For now, I have created a class and solved the issue discussed. I'm a beginner, and any book/resource recommendations would be welcome!

Upvotes: 0

Views: 1372

Answers (2)

Walter
Walter

Reputation: 45414

Your code does not what you think it does.

// file engine.hpp
namespace render {
  void Initialise();
  namespace inits {
    bool glfw_init = false,     // these variables will be emitted with
    glfw_window_init = false,   // every source file that
    gl_init = false;            // #include "engine.hpp"
  }
}

will lead to errors ("mulitply defined symbols") at linking/loading. To avoid that you may declare the variables extern or use functions for setting/getting the state (stored in a variable defined in engine.cpp).

Upvotes: 0

tinkertime
tinkertime

Reputation: 3042

You're using nested namespaces, so should be:

render::inits::glfw_init = false;

Upvotes: 1

Related Questions