How is a Space that Namespace calls for Defined?

Can someone help understand some Structure here?

It was mentioned in this lecture that a function or variable, could be in two different spaces in an include (a library or a .h file I am assuming).

So how is a “Space” defined? Is this like a section inside a given Library or a .h file or another .cpp file?

This relates to the issue of utilizing the “using namespace” statement.

Why would one define the same function or variable two different ways inside the same document of even in separate documents? Why not simply call it something different?

How can there be two different ways for a function such as “cout”, maybe they are differently syntaxed but basically they should be doing the same thing with same outcome no?

And if that is the case, then why doesn’t the compiler just choose the first and ignore the rest?

Thanks for any clarifications.

It was mentioned in this lecture that a function or variable, could be in two different spaces in an include (a library or a .h file I am assuming).

That would be namespace, you can define your own namespaces and use them like

namespace Dan
{
    std::string GetDan() { return "Dan"; }
}

int main()
{
    std::cout << Dan::GetDan();
}

Why would one define the same function or variable two different ways inside the same document of even in separate documents? Why not simply call it something different?

You might not have been the one who wrote that code, e.g. it was defined in a library you are using.

How can there be two different ways for a function such as “cout”, maybe they are differently syntaxed but basically they should be doing the same thing with same outcome no?

So an example I came across recently was over here:

Since the code wasn’t formatted I figured I would just copy and paste the code in VS to make it readable and then since I’m already there, debug it. The problem code being the with for loop, so I just thought to log the count as it’s quick and dirty.

for (int count = 1; count <= 5; count++);
{
	GetGuessAndPrintback();
	cout << count << endl;
}

Now the problem is the semi-colon before the braces, so the code in the braces isn’t within the scope of the loop so will only happen once. Though I don’t get the expected error of ““count” undeclared identifier” (meaning it doesn’t know what count is seeing as it’s not in the same scope of the loop) I get a completely different error saying “binary ‘<<’: no operator found which takes a right-hand operand of type ‘overloaded-function’ (or there is no acceptable conversion)”. And as you might have guessed that’s because there’s a function called count in the std namespace.

Hello,

Thank you for the clarifications :slight_smile:

Lets see if I understand correctly, So in the count example, that you refer to, the “conflict” happens as a result of the error with the “;”. But if the error is corrected then the correct “count” will be returned yes?

Un other words, the fact that there is another function called “count” in the std namespace is not an issue unless there is a problem such as this case?

Is this the case?

Cheers!

Correct, if you remove the semicolon it will function properly and use the correct count. Though the error message that had nothing to do with what was wrong with the code might lead someone to waste their time going down the wrong path trying to debug their code.

Took me a few seconds to get what was wrong with it as I don’t use using namespace std

Ok! I was a bit apprehensive about the whole issue of Namespaces. Since we are instructed to use them and then we are told “but beware…” without necessarily understanding why. I was under the impression that one has to verify that the namespaces they sue are not present and defined differently in any library they use etc etc in their projects and I was like ok then “just don’t use it in future… why complicate things?”

Yet now I understand with your help that the problem with namespaces then is specific to the case of causing problems during debugging as it can misdirect someone or cause them to look in the wrong place.

Thanks! :slight_smile:

1 Like

I think there’s some misunderstanding “using namespace std” isn’t creating a namespace, all that statement is doing is putting everything in that namespace in the global one so you won’t have to qualify it with std::

For example in the first response I gave i created a namespace called Dan and had a function called GetDan within that. If I ever want to use that function i would have to always qualify it with Dan:: . However, if I put using namespace Dan; everything in Dan is now in the global space and I can now just call GetDan without the Dan::

I understand that part. Like Why we use the namespace, easier to type cout << etc than std::cout << etc.

The confusion came with the Warning that followed this notion of using a namespace. There was confusion as to the nature of what we have to be aware and beware when using namespaces like that.

So let me put it differently…

What is the danger of using namespaces? Lets say that I call using namespace Dan; but the function GetDan exists also in std and I also do using namespace std;, then would that cause an issue? or will it only cause an issue if I have a bug in relation to GetDan function in my code making it harder for me to debugg?

Yes that would probably cause a redefinition compilation error. Also your continuation of my silly Dan and GetDan example amuses me :slight_smile:

1 Like

Hehe, well for relevance and better mutual understanding.

Yet, what is the exact case of the conflict, the fact that GetDan exists in two places and both are used as namespaces or the later case? It still not 100% clear what I need to be aware of when using namespaces, what to avoid doing so that no issues arise.

Maybe it is too early to understand because I am missing so many other notions yet.

I know this message is a little old but I agree with Suraknar. I get the whole reasoning why. It just a little annoying that the whole problem exist at all. The programmers couldn’t come up with unique names for each function? I understand the need to be succinct and for code to be self explanatory but that doesn’t stop the grumblies. Also it would be nice if Ben gave an actual example of namespace conflicts. Right now everything seems so abstract.

Also anyone else get the “deer-in-the-headlights-look” when Ben gives the “Try it yourself” thing? I just started learning how to program and I’m already ask to do the tutorial phase myself and then go “one step more” when I’m still fumbling around. Needless to say, I was one of those people who tried to create a main.cpp the hard way and raged when the file wouldn’t link with the solution. :frowning:

And how would you co-ordinate with all these people who you are using code from or people using your code to ensure you all have unqiue names for everything? It would be literally impossible. Also take Boost for example, a very popular C++ library, if there were no such things as namespaces there would be a LOT of name clashes.

Sorry, I didn’t notice you replied. Basically avoid using namespace x and you’ll be fine.

I see your point. I’m not saying to do away with namespaces just have a unique indentifier for the functions they contain. It’s a moot point to argue as no one will change long held convention for n00bs. It’s just nice to vent.

Haha. Papercut2k agreed. :slight_smile:

So in conclusion even using namespace std is to be avoided?

Thanks to both.

I would whilst your learning; maybe when you are more experienced you can better decide if/when to use that.

Ok great, thanks! :slight_smile:

I just went through al the removal of namespace in the current project.

So I understand why it should never be used in a .h file (just thinking of a big program caused a mental storm), I have a better understanding now why not use namespaces all together.

So it only leaves a question at the Theoretical level, why do we have that capability in the first place?And why is it taught to every single Book I opened when in reality later on it is discouraged?

Just do not teach C++ with Namespace anymore and be done with it. Abandon the usage of it and not even mention it. No?

Just to be clear for you or anyone else reading, namespaces are good “using namespace x” i.e. putting everything in the x namespace into the global space, isn’t.

I know that Bjarne’s books use them only because it’s to save valuable space on a page and I’m fairly certain he discourages you from actually using it. Not sure on other books.