Hi everyone,

Im working on a large scale audio program, and I got a question regarding namespaces:

The main() part of the program includes my own headers, each named according to what part it plays in the program, eg: Audio.hpp , Midi.hpp etc. These headers will "load" other headers..? Is this illegal? or can the following be done/fixed?

Inside these headers, i will have a "global" namespace. Best way to describe this is:

audio.hpp

namespace Audio
{
	// Audio Include
	#include <iostream>
	int jack_create_client()
	{
	   std::cout << "In jack_create_client() " << std::endl;
	}
}

Code in Engine.cpp

namespace Engine
{
	#include "audio.hpp"
}

Now say i have a main.cpp, and in it is this:

//#include <iostream>	// Uncomment, and it compiles.
#include "engine.hpp"

int main(int argc, char** argv)
{
	Engine::Audio::jack_create_client();
	return 0;
}

However uncommenting that line exposes iostream to the main.cpp file. (in this case its not bad, but if im using some audio processing function, it could name-clash with a similar function for midi.)

Any Idea how i can call jack_create_client() in a similar way to Engine::Audio::jack_create_client() without this problem??

Hope i made the question clear, i realize its a little complex to explain!
-Harry

>>These headers will "load" other headers..? Is this illegal?
Yes. Many standard system header files include other header files.

>>in this case its not bad, but if im using some audio processing function, it could name-clash with a similar function for midi.)
There will be no name clashes because that's exactly one of the purposes of namespaces. Everything in <iostream> is in std namespace.

>>Any Idea how i can call jack_create_client() in a similar way to Engine::Audio::jack_create_client() without this problem??

What problem? If jack_create_client() is in global namespace then just call ::jack_create_client() and that will be different then the function with the same name but in namespace Autio.

Hey Dragon,

Thanks for the reply, but the problem is that the above code throws 211 errors on compiling. They're all fixed if I uncomment the #include <iostream> in the main.cpp file.

And i dont understand why, as im #include <iostream> -ing just before i use the function. And (as you mentioned) the whole idea of namespaces is to not have to have <iostream> included in main.cpp as well.

Im at a loss here, sorry! -Harry

Hey again,

The code above is the code im trying to make work. Its included as a Microsoft Visual Studio 2005 project file, but otherwise just grab the 3 source files & do whatever it is you do!

However, I just tried the same thing with a different header than <iostream> and its all good & compiling.. is <iostream> something magic that it must be included for some reason? I mean i know its the InputOutputSTREAM...

anyway, ill flag this as solved for now. Im not fully sure on why it wasnt working earlier though! Cheers Dragon, -Harry

Attached is the "old" version that was not compiling for me.. Im on linux using MonoDevelop, so maybe I got the enviroment slightly wobbely... Duno!

namespace A
{
  #include <iostream>
}

You can't include iostream header file into custom namespaces. The problem is that the qualified names inside the namespace would be A::std::cout, but the library would not contain names qualified with the outer namespace.
However, you may declare header file outside the namespace and use using statement inside the custom namespace.

#include <iostream>
namespace A
{
  using namespace std;
}
int main()
{
      A::cout << "\nHello";
     return 0;
}

http://msdn.microsoft.com/en-us/library/xb3bx4w8(VS.71).aspx

This question has already been answered. Start a new discussion instead.