So this is what I have:
//graphics.hpp
#include guard
extern camera_c default_camera;
namespace graphics {
camera_c &camera = default_camera;
};
#endif
//graphics.cpp
camera_c default_camera(ctor stuff);
//main.cpp
#include <graphics.hpp>
int main() {
do stuff with graphics::camera;
}
and this gives me
main.o: multiple definition of graphics::camera
graphics.o: first defined here
I’ve also tried doing
camera_c &&camera = camera_c(ctor stuff);
And that gives me the same error, but from main.cpp and graphics.cpp
So my question is
Is there some feasible way to have a reference to a class in that namespace? Or should I just use a pointer? Ideally it should be a reference, but that might not be possible.
You’re misunderstanding the error.
The error is telling you that main.cpp and graphics.cpp both included graphics.hpp, which violates the One Definition Rule.
Make that variable
externas well:And define it in one source file only.