Thread: Cant use std namespace, help required

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    23

    Cant use std namespace, help required

    I am unsure if this is a common problem, but i use VC++ 6.0, i am still learning, but i cant seem to use anything from the std namespace.
    If i type std:: then the IDE gives me a list of everything in it, as though it is there, but when i try to compile it gives the error: std is not a namespace. Why is this? how do i use it?
    even in this simple (barely a) program it doesn't work:
    Code:
    int main(void)
    {
    	std::string mystring;
    	return 0;
    }
    thanks for helping,
    esaptonor

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What libraries are you including? Are you including <iostream> or <iostream.h>, for example? The former uses the std namespace, the latter doesn't. There for, if you're including the latter libraries (with the .h extension) then you'll never have the std namespace defined in your code.
    Sent from my iPadŽ

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Get a compiler that conforms to standards... I do believe VC++ 6 you would just use string myString;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    even in this simple (barely a) program it doesn't work:
    It shouldnt work with any compiler in the world.

    I suggest you write:
    Code:
    #include <string>
    
    int main()
    {
    	std::string mystring;
    	return 0;
    }
    Get a compiler that conforms to standards... I do believe VC++ 6 you would just use string myString;
    Even MSVC6 is aware of namespaces.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Ah, kk, I know VC++ 6 does not conform to standards in a number of ways... just didn't feel like looking up the list.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I don't think the issues with Visual C++ 6 goes beyond warning you when it shouldn't and not warning you when it should... I'm pretty sure as far as errors go, it's as spot on as any other modern compiler. I could be wrong on that, though.
    Sent from my iPadŽ

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Have you tried -

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main( void )
    {
    	string mystring = "Namespace";
    	cout<< mystring;
    
    	return 0;
    }

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    23
    thanks guys, i was using iostream.h, or nothing that defined it in, so once i included iostream or string it worked....i had only tried string.h, which doesn't work.

    thanks again,
    esaptonor

  9. #9
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Read this about <iostream.h> vs <iostream>

    http://www.devx.com/tips/Tip/14447

    I don't think that iostream.h was part of the standard, it was only used during the drafting
    process. Nevertheless, it is a bad idea to use it since it is very outdated.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  10. #10
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    Quote Originally Posted by Richie T
    I don't think that iostream.h was part of the standard
    Anything with '.h' doesnt support namespaces. So when you add 'string.h' instead of just 'string', then std::string will not work.
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So when you add 'string.h' instead of just 'string', then std::string will not work.
    Correct, but not because of the std namespace. string.h is a C header with C string handling functions. It never has and likely never will host the string class.
    My best code is written with the delete key.

  12. #12
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    >>Correct, but not because of the std namespace
    sorry my bad....was getting mixed up between the string and the cstring headers.

    so to undo what i did:

    include 'string' to add string class declared in std namespace
    now std::string will work

    include 'cstring' (which is the new 'string.h') for c-string functions declared in global namespace so no need for a using statement (or scope resolution) specifically for those functions
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> include 'cstring' (which is the new 'string.h') for c-string functions declared in global namespace so no need for a using statement

    Actually the names inside the C++ versions of C library headers are all supposed to be in the std namespace as well. Some compilers still allow them to be accessed without the namespace, though.

    >> I'm pretty sure as far as errors go, it's as spot on as any other modern compiler.

    Not really. While it is fairly close in many regards, there is a significant amount of code that compiles on VC++ 6 that won't compile on modern compilers, and vice versa. The biggest issue is with templated code, but there are other differences as well.

  14. #14
    Useless Apprentice ryan_germain's Avatar
    Join Date
    Jun 2004
    Posts
    76
    ok good to know...

    this runs on VC++6 so i imagine the cstring functions are in the global namespace for VC++6 though

    Code:
    #include <cstring>
    #include <iostream>
    
    int main(int argc, char* argv[])
    {
    	std::cout << strlen("hello");
    	return 0;
    }
    There is not the slightest indication that [nuclear energy] will ever be obtainable. It would mean that the atom would have to be shattered at will.

    -Albert Einstein, 1932

  15. #15
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >this runs on VC++6 so i imagine the cstring functions are in the global namespace for VC++6
    Of course, VC++6 is infamous for being horribly incompatible with standard C++.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thoughts about using namespace std?
    By dwks in forum C++ Programming
    Replies: 40
    Last Post: 09-25-2008, 05:14 PM
  2. using namespace std;
    By spank in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 06:28 AM
  3. namespace std
    By strobe9 in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2003, 08:09 PM
  4. using namespace std, What does it do?
    By Ben K. in forum C++ Programming
    Replies: 1
    Last Post: 10-14-2001, 10:42 PM
  5. What is "using namespace std; "
    By Engel32 in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2001, 07:23 AM