C++ namespace issue

Started by
9 comments, last by KulSeran 14 years, 3 months ago
I'm porting a C# project to C++ and am having issues with namespaces. Here is a typical file in the project
#pragma once

using namespace Flint::Actions;
using namespace Flint::Activities;
using namespace Flint::Counters;
using namespace Flint::Easing;
using namespace Flint::Emitters;
using namespace Flint::EnergyEasing;
using namespace Flint::Initializers;
using namespace Flint::Particles;
using namespace Flint::Zones;

namespace Flint
{
	namespace Particles
	{
		class IParticleFactory
		{
			...
		};
	}
}
As you can see it's broken up into a bunch of namespaces with classes in them. The problem is I am getting the following errors
'Flint' : is not a class or namespace name
'Actions' : a namespace with this name does not exist
It's saying there are no namespaces, yet typing in Flint:: the intellisense lists all the namespaces I expect under "Flint". [Edited by - Headkaze on January 18, 2010 3:32:39 PM]
Advertisement
What are the exact lines of code that produce that error? Can you post more context, e.g. showing how your #includes are set up and so on? Seems to me like you've just got some using declarations before the namespaces are seen, but it's not really possible to tell where the mistake lies without seeing more code.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

It's a bit hard to understand where each of these lines of code are copied from.
Is this in "flint.h"?
 #pragma oncenamespace Flint

Is this in "flint.cpp"?
#include "flint.h"using namespace Flint::Actions;
I'll try and give more info

-- ParticleFactory.cpp (Original C# file) --

using FlintSharp.Actions;using FlintSharp.Activities;using FlintSharp.Counters;using FlintSharp.Easing;using FlintSharp.Emitters;using FlintSharp.EnergyEasing;using FlintSharp.Initializers;using FlintSharp.Particles;using FlintSharp.Zones;namespace FlintSharp.Particles{    public interface IParticleFactory    {		...    }}


-- ParticleFactory.h --

#pragma onceusing namespace Flint::Actions;using namespace Flint::Activities;using namespace Flint::Counters;using namespace Flint::Easing;using namespace Flint::Emitters;using namespace Flint::EnergyEasing;using namespace Flint::Initializers;using namespace Flint::Particles;using namespace Flint::Zones;namespace Flint{	namespace Particles	{		class IParticleFactory		{			...		};	}}


-- ParticleFactory.cpp --

#include "..\Particles\ParticleFactory.h"using namespace Flint::Actions;using namespace Flint::Activities;using namespace Flint::Counters;using namespace Flint::Easing;using namespace Flint::Emitters;using namespace Flint::EnergyEasing;using namespace Flint::Initializers;using namespace Flint::Particles;using namespace Flint::Zones;namespace Flint{	namespace Particles	{	}}


This is just an example but I'm seeing the same error on every "using namespace Flint::xxx" line.

[Edited by - Headkaze on January 18, 2010 3:43:29 PM]
In C++, you have to provide some contents for a namespace before you can refer to it via using. Also, placing using directives in your headers (vs. in your .cpp files) is considered bad form, as it forces all files using that header to import the "used" namespaces, rather than leaving that control up to the author of the final .cpp file itself. In other words, if you're just going to using everything from a header anyways, why bother with namespacing?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Also take into account that you cannot be "using" a namespace before that namespace has been declared. Consider:

using namespace Test;namespace Test{	int x;}


This will result in an error, since you're trying to "using" something that doesn't exist yet. First, heavily consider removing all "using" declarations. As has been stated, placing those in the header more-or-less completely removes the point of namespaces in the first place.

If you must have them, only "using" the namespace that was declared in that file, and do it after the declaration.

namespace Test{	int x;}using namespace Test;


Like so. If the client needs Flint::Actions or Flint::Activities, he would include those headers respectively. Those no reason to promote their scopes from a completely unrelated header.
This is pretty frustrating. Namespaces are so straight forward to use in C#. The only reason why I'm using them is because in the C# port of Flint ("FlintSharp") I used the same namespaces as the original AS3 version. Now in the C++ port I thought it would make sense to keep those namespaces. Now I'm starting to think it may be best to move them all out into one "Flint" namespace. It seems namespaces are not used in the same way as they are in C#.
Namespaces are just as valuable in C++ as in C#. The only thing that's gone wrong here is your split up of the namespaces and using declarations in your header files. There's no need to jettison the entire namespace system just because you copy/pasted a couple lines to the wrong file by accident [smile]

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Although I would say that C++ doesn't really lend itself to deeply nested namespaces - certainly not to the extent that C# does. Most libraries just use the one namespace (std or boost, etc) and maybe one more for "detail" that you don't want pollute the "real" namespace.
Really appreciate the help here guys. So I started moving all the classes out of their sub namespace and then I discover to my horror that some of the classes have the same name. Now I'm in a bit of a pickle.

What do people suggest I should do? What would be the correct way to port this to C++? To have one namespace and rename the duplicate classes? Or keep the nested namespaces and avoid name collision?

Here are examples of repeating class names:

http://code.google.com/p/flint-sharp/source/browse/#svn/trunk/src/twoD/Easing
http://code.google.com/p/flint-sharp/source/browse/#svn/trunk/src/twoD/EnergyEasing

Since there is alot of code to port I would like to hear some professional advice before I go ahead with it.

This topic is closed to new replies.

Advertisement