Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9201141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:56:04+00:00 2026-06-17T22:56:04+00:00

So, I have the following in a specification file #include <string> #include <fstream> using

  • 0

So, I have the following in a specification file

#include <string>
#include <fstream>
using namespace std:

class MyStuff
{
    private:

    string name;
    fstream file;
    // other stuff

    public:
    void setName(string);
}

I also have in the implementation file

#include "MyStuff.h"
using namespace std;

void MyStuff::setName(string name);
{
     name = name
}

and in the program file I have…

#include <iostream>
#include <string>
using namespace std;

void main()
{
     string name;
     MyStuff Stuff;

     cout << "Enter Your Name: ";
     getline(cin, name);

     Stuff.setName(name);
}

And I’m gathering that applying “using namespace std;” in a header file is a no-no, and that to fully qualify is the “better” practice; such as std::cout << stuff << endl;

It is my understanding that in order to use a string, it must have the std namespace. Is that true?

If so, in the header file, is more “pure/clean” to do it as…

#include <string>

class MyStuff
{
     std::string name;
}

And, as I understand it currently, using namespace std; across all three files, specification, implementation, and program, essentially layers the three namespaces on top of each other, so if I separately declare string name; within each of the files, the compiler will not know which goes to what. Is that true?

I generally understand that being clear is a “good” thing to do, I am however a little unclear on the specificity of how, and I’m most interested in the deeper “why” that underlies it all.

So my direct question is, in my example provided, what is the “clearest” way to describe the function both for the compiler and for industry “standard”? And, can you direct me to resources that more clearly delineate the reasoning and practical implementation of namespaces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:56:05+00:00Added an answer on June 17, 2026 at 10:56 pm

    Let’s say I declare a class string myself. Because I’m a lazy bum, I do so in global namespace.

    // Solar's stuff
    class string
    {
        public:
            string();
            // ...
    };
    

    Some time later on, I realize that re-using some of your code would benefit my project. Thanks to you making it Open Source, I can do so:

    #include <solarstuff.hpp>
    #include <phoenixstuff.hpp>
    
    string foo;
    

    But suddenly the compiler doesn’t like me anymore. Because there is a ::string (my class) and another ::string (the standard one, included by your header and brought into global namespace with using namespace std;), there’s all kinds of pain to be had.

    Worse, this problem gets promoted through every file that includes my header (which includes your header, which… you get the idea.)

    Yes I know, in this example I am also to blame for not protecting my own classes in my own namespace, but that’s the one I came up with ad-hoc.

    Namespaces are there to avoid clashes of identifiers. Your header not only introduces MyStuff into the global namespace, but also every identifier from string and fstream. Chances are most of them are never actually needed by either of us, so why dragging them into global, polluting the environment?

    Addition: From the view of a maintenance coder / debugger, foo::MyStuff is ten times more convenient than MyStuff, namespace’d somewhere else (probably not even the same source file), because you get the namespace information right there at the point in the code where you need it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following in an implementation file... void ClientList::interestCompare(vector<string> intr) { for(int index
I have to read properties file MyProperty.properties from ReadProp.java class given my the following
I have to read properties file MyProperty.properties from ReadProp.java class given my the following
I have following nested objects. I am using @Valid for validation in my controller.
I have the following sample code (a stripped down version of my programme) Class
I have the following records in my projects's .classpath file: <classpathentry kind=src path=src/main/webapp output=build/webapp/>
I have two entities: ResourceFile: @Entity @Table(name = resource_file) public class ResourceFile extends IdEntity<Integer>
I am using rails 3.2.9 and database of postgres. I have edited database.yml file
I'm new to Scala, SBT and Specs. I have created a simple specification using
I have the following code to import a delimited file into an Access 2003

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.