I am using namespaces from a long time now. But, I don't know why we need them at the end? Why do I need to write

using namespace std;

OR

std::string

Why do we have to do this? Why have they made it this way? Can you elabroate this thing? Thanks.

Edited 2 Years Ago by nitin1

The namespaces lets us to group a set of global classes, objects and/or functions under a specific name. If you use the using namespace std in the code then you don't need to put std:: throughout your code. The program will look in the std library to find the object. Namespace std comprises all the classes, objects and functions of the standard C++ library.

namespaces is like a cointainer to hold all your classes, function...etc
its specially used to prevent name conflicts.
you may work on a project with someone and may both of you have a class or function with same name so you can use namespaces to prevent that.
like C++ standard libraries written in namespace std and Qt library in Qt namespace

namespace foo{
    class me{
    };

    void bar()
    {}
}

namespace woo{
    class me{
    };

    void bar()
    {}
}

foo::bar(); //calls first bar
woo::bar(); // calls second bar

But, when I use using namespace std, then I also have to use "string" header file. So what is the difference between them?

If everything is defined in namespace, then we need header and cpp files? Can you please clear the difference between all of them?

Also,
I have written this temporary code which as one error:

#include<iostream>
#include<cstring>

using namespace std;

namespace  temp
{
           class string
           {
                 public:
                        void foo()
                        {
                             std::cout<<"hello in namespace foo()"<<std::endl;
                        }
           };
}

int main()
{
       temp::string str;
       std::string g ="string.h wala string";

       std::cout<<g.find("wala")<<std::endl;
       std::cout<<str.foo()<<std::endl;

    system("pause");

}

it is showing one error on second last line. Why is it not working? It says

24 C:\Users\gourav.si\Desktop\beecrypt-4.2.1\Untitled1.cpp no match for 'operator<<' in 'std::cout << (&str)->temp::string::foo()' 

Thanks.

Edited 2 Years Ago by nitin1

But, when I use using namespace std, then I also have to use "string" header file. So what is the difference between them?

The header provides you with declarations. using namespace std simply tells the compiler to pretend you had qualified a name with std:: if it can't resolve the name from the global namespace. The declaration is still required, otherwise the name won't resolve regardless of which namespaces you open up.

If everything is defined in namespace, then we need header and cpp files?

Yes. The thing has to exist before you can start monkeying around with how you refer to it.

I have updated my comment. I have added one problem in the last comment. Please see that also.

Secondly, why don't we have namespce thing in C? Why do we need it in C++? When namespaces are resolved in the cycle of compilation to execution? Thanks.

I have updated my comment. I have added one problem in the last comment. Please see that also.

It would be better to do that in a new reply rather than interleave new stuff into the thread. But anyway:

std::cout<<str.foo()<<std::endl;

What does foo return?

Secondly, why don't we have namespce thing in C?

Probably because nobody came up with a good solution to the problem before C++ came out and became popular.

Why do we need it in C++?

Because it's an obvious pain point in C, and C++ was originally intended to be a "better" C.

When namespaces are resolved in the cycle of compilation to execution?

Hmm, I do not know (or if I ever did, can't remember).

the main meaning of the given namespace

namespace namespace_name

// code declarations

and call the name sapceenabled version of function and variable prepend the name space name as given below..

name::code;

// code could be variable or function.

Edited 2 Years Ago by deceptikon: Snipped spammy link

Actually, I am confused that what the problem was there in C and how c++ has solved it. Having namespace solves our problem by resolving the names.

For ex: if I say 'string' that means diffferent if I say std::string. Right? Here, names are resolved because everything is defined in std namespace? So is this what you are telling?

Headers has just the declarations as I already know. namespaces include all the definitons? right? Please make me correct if I am wrong at any place. Thanks a lot, James Sir.

Actually, I am confused that what the problem was there in C and how c++ has solved it.

Let's say you have two libraries with a print function in C. How do you use them both at the same time? The answer is you don't, at least not without writing a wrapper library of your own that uses unique names to resolve the conflict. In C++, those functions would (should!) be wrapped in a namespace and there's no problem from the start.

For ex: if I say 'string' that means diffferent if I say std::string. Right?

Conceptually, yes. It's complicated with things like using namespace std, but the core of namespaces is indeed to ensure a unique name.

Headers has just the declarations as I already know. namespaces include all the definitons? right?

Namespaces don't include anything. A more accurate way to describe it would be that headers contain declarations and declarations may be wrapped in namespaces to guarantee uniqueness.

The namespace is basically a container for a set of identifiers may be you know name and symbols and namespace is provide a level of direction to specific identifiers.You can also visit our IT Company in lucknow.

Edited 1 Year Ago by Itecg

This article has been dead for over six months. Start a new discussion instead.