Namespace question....

This is a discussion on Namespace question.... within the C++ Programming forums, part of the General Programming Boards category; Trying to get this old noggin around Namespaces.... Code: #include <iostream> #include <string> int main() { { using namespace std; ...

  1. #1
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Namespace question....

    Trying to get this old noggin around Namespaces....
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      { using namespace std;
        static string name;
        cout << "What is your name? ";
        getline(cin,name);
        cout << "Hello, " << name;  }
    
        std::cout << "Are you really, " << name;
        return 0;
    }
    Is there a way for the last cout to access name from the namespace above?
    I either get errors saying there's no such variable or that it's not a member of std...

    LOL... and I thought classes were confusing....

  2. #2
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    That's not your problem:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
      { 
        using namespace std;
        static string name;
        cout << "What is your name? ";
        getline(cin,name);
        cout << "Hello, " << name;  
      }
    
        std::cout << "Are you really, " << name;
        return 0;
    }
    Clearer?

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... no.... I know the namespace went out of scope... that's why I made name static... Now the question is, how to I access it from the last line?

    std::name does not work....

    Or does static only work within the namespace scope?

    Long story... for me (and probably only for me) the way I formetted it the first time is clearer...

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    Not only did the namespace go out of scope, the symbol itself went out of scope with the closing brace.

    The object still exists, but you have no way to refer to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you might have to put the
    Code:
       using std::cout ; //or using namespace std ;
    just after the #include statements
    You ended that sentence with a preposition...Bastard!

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You didn't put the variable name into any namespace, nor did you create a namespace. Using doesn't put things into namespaces, it allows you to use variables from other namespaces in your current context. To put something into a namespace you have to do something like
    Code:
    namespace hey_ma_its_a_namespace {
        static string name;
    }

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    16
    Umm.. Seriously?

    Why would you declare the namespace WITHIN main?

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    {
        //using namespace std;
        static string name;
        cout << "What is your name? ";
        getline(cin,name);
        cout << "Hello, " << name;  
    }
    
        cout << "Are you really, " << name;
        return 0;
    }
    Personally, I would do:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
         string name;
    
         cout << "What is your name?
         cin >> name;
         cout << "Hello, " << name << "!" << endl;
         return 0;
    }
    Last edited by User-_-Name; 01-16-2011 at 01:48 PM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by User-_-Name View Post
    Umm.. Seriously?

    Why would you declare the namespace WITHIN main?
    For the learning experience...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    You didn't put the variable name into any namespace, nor did you create a namespace. Using doesn't put things into namespaces, it allows you to use variables from other namespaces in your current context. To put something into a namespace you have to do something like
    Code:
    namespace hey_ma_its_a_namespace {
        static string name;
    }
    AHHH... now it sinks in... thanks.

    So it's like salem said... it's not the namespace at all... the variable simply went out of scope... and now I can't access it.

    Thanks guys! Much appreciated.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Not only did the namespace go out of scope, the symbol itself went out of scope with the closing brace.

    The object still exists, but you have no way to refer to it.
    So, if that was in a loop... I'd still have the name next time around?
    See... now it's starting to sink in!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    namespace test
       { static string name; }
    
    
    int main()
    { {  using namespace test;
         cout << "What is your name? ";
          getline(cin,name);
          cout << "Hello, " << name << endl; }
    
        cout << "Are you really, " << test::name;
        return 0;
    }
    Now it works... thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. namespace tm?
    By cunnus88 in forum C++ Programming
    Replies: 3
    Last Post: 01-28-2009, 02:02 PM
  2. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  3. namespace question
    By Dave++ in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2007, 05:47 PM
  4. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 02:10 PM
  5. using namespace std; question.
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 03:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21