Sign in to follow this  

[C++ / Irrlitch] Namespace problem

Recommended Posts

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;

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this