[C++ / Irrlitch] Namespace problem

Started by
3 comments, last by Spoonbender 16 years, 11 months ago
Okay, I'm still learning things every day w/ C++ and GCC. I'm using Irrlitch, and I've been working with some of the examples. Variables were initially preceded by their respective namespace info but I want to declare the namespace. However I am running in to a problem, after I declare the namespaces it interferes with the std namespace, saying that string type isn't defined, and I get the following compile error... 17 C:\Dev-Cpp\main.cpp expected constructor, destructor, or type conversion before '*' token Any thoughts?

#include <cstdlib>
#include <iostream>
#include <vector>
#include <irrlicht.h>

using namespace std;

using namespace irr::core;
using namespace irr::io;
using namespace irr::video;
using namespace irr::scene;
using namespace irr::gui;

IrrlichtDevice 		*IrrDevice;
IVideoDriver	*IrrDriver;
ISceneManager	*IrrSceneManager;


Advertisement
This situation is exactly what namespaces are intended to solve. That is, libraries that have the same names. In this case, there's a class named "string" in both std and irrlicht::core. So then when you declare something as type "string", the compiler has no idea which you're referring to.

You'll need to expicitly tell the compiler which you want, by declaring the object as "irrlicht::core::string" or "std::string". It sounds like you don't want to do this, but you don't have much choice for classes with the same name. Fortunately, you only need to do it in this case. Other classes with unique names don't need to be preceded by the namespace.
Oh the irony, lol. Thanks for your help, very appreciated. :)

gharen2++;
Note, I bypassed the problem by not declaring the irr::core namespace.
using <namespace> is generally considered a Bad Idea, and your example showed exactly why.

Of course, having to type irr::video::whatever might be a wee bit verbose, but then you could do something like this:
namespace iv = irr::video;

and then you only have to prefix everything with iv::

Alternatively, include the irr namespaces, but don't do it with the std namespace.

This topic is closed to new replies.

Advertisement