Thread: what's wrong with this piece of code

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    what's wrong with this piece of code

    Hi

    i am trying to write a simple c++ program

    why does this code throw up an error

    Code:
    #include "stdafx.h"
    
    //using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	cout << "C++ is better than C\n";
    	
    	return 0;
    }
    the error being
    e:\softwaredevelopment\Cpp\myapp101\myapp101.cpp(1 0): error C2065: 'cout' : undeclared identifier
    whereas this piece of code works ok

    Code:
    #include "stdafx.h"
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	cout << "C++ is better than C\n";
    	
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Because the standard output device, cout, exists in a namespace called std.

    The "using namespace std;" directive causes the compiler, when it encounters the name "cout" to look for something that has that name within namespace std. It therefore resolves cout to std::cout. Commenting out the "using namespace std;" directive means that the compiler is not required to look in namespace std to find a match.

    Incidentally, stdafx.h and _tmain are both windows specific.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    70
    is namespace kind of a library or does it just describe/define some scope? i tried to do a bit of reading and i think it describes scope. does this mean that once using namespace std is mentioned, functions in the stanadrd library are global thtoroughout the program or are they just visible in the file which contains using namespace std?

    by the std namespace we mean functionsin the stdlib.h, correct, but isn't the cout and cin functions of the iosteam.h library, i nthat case whould it not have made only functions in stdlib.h visible

    in that case by including <iostream.h> should the program not work?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A namespace is literally a "space of names". It would be closer to what you are describing as "some scope".

    The standard output devices named cin and cout are declared in a header named <iostream> not <iostream.h>. <iostream.h> dates back to a very early (pre-standard) version of the C++ standard library, and has been superseded by <iostream>. If your compiler/library has an <iostream.h>, including it may work as that header predates namespace std, so objects like cin and cout were available outside of namespace std. However, no C++ compiler is required to support the <iostream.h> header so that approach is not guaranteed to work with other compilers (or, indeed, it might stop working with a future version of your compiler).

    <stdlib.h> is a standard C header, not a C++ header, although it is supported by C++ for backward compatibility (and also deprecated to discourage it's use in production code). The "std" in the header name is coincidental, and has no relationship to the std namespace. In C++, the encouraged method of accessing function in C's <stdlib.h> is to #include <cstdlib>. One of the differences between <stdlib.h> and <cstdlib> is that (a lot) of the functions and variables in <cstdlib> are also placed in namespace std.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by studentc
    is namespace kind of a library or does it just describe/define some scope? i tried to do a bit of reading and i think it describes scope. does this mean that once using namespace std is mentioned, functions in the stanadrd library are global thtoroughout the program or are they just visible in the file which contains using namespace std?
    Yes, it basically defines a scope that the objects are contained within. To access objects within a namespace, you can do one of three things:

    #1 - put a "using namespace" declaration in your code
    Code:
    namespace foo
    {
        int x;
        double d;
    }
    
    using namespace foo;
    
    int main()
    {
        x = 5;    // Assign the value 5 to foo::x
        d = 10.0; // Assign the value 10.0 to foo::x
    
        return 0;
    }
    #2 - Identify the individual member of the namespace you want to have access to with individual "using" declarations.
    Code:
    namespace foo
    {
        int x;
        double d;
    }
    
    using foo::x;
    
    int main()
    {
        x = 5;         // Assign the value 5 to foo::x
        foo::d = 10.0; // Still need foo:: in front of d because
                       // we did not say we were using foo::d like we did with foo::x
    
        return 0;
    }
    #3 - Explicitly qualify the namespace everywhere you use it
    Code:
    namespace foo
    {
        int x;
        double d;
    }
    
    int main()
    {
        foo::x = 5;     // Assign the value 5 to foo::x
        foo::d = 10.0;  // Assign the value 10.0 to foo::d
    
    
        return 0;
    }

    Quote Originally Posted by studentc
    by the std namespace we mean functionsin the stdlib.h, correct, but isn't the cout and cin functions of the iosteam.h library, i nthat case whould it not have made only functions in stdlib.h visible
    You're a little confused. The headers typically contain only function prototypes and #define statements predominately. The actual code for these functions exist in what are called libraries that are "linked" after compilation to produce the final executable program. So, iostream is not a library but rather simply a header. These headers are mostly used by the compiler to perform the task of type-checking, validating that the correct number of arguments have been passed and so on.

    Headers with the old style ".h" extension in them are the older version of the newer headers (the ones without the .h extension). If you use the old headers, you will not have to do anything with regards to namespaces for the functions that are declared within them. The newer headers declare their contents within the std namespace.

    In addition, the newer version of the standard C headers also have a "c" prepended to them... as an example:

    Code:
    Old header    New header
    <iostream.h>  <iostream>
    <stdlib.h>    <cstdlib>
    <stdio.h>     <cstdio>
    <string.h>    <cstring> (not to be confused with <string> which is different entirely)
    Quote Originally Posted by studentc
    in that case by including <iostream.h> should the program not work?
    As mentioned, iostream.h is the old version of the header and the functions/objects defined in it aren't in any namespace, thus you could do this:
    Code:
    #include <iostream.h>
    
    int main()
    {
        cout << "Hello world!" << endl;
        return 0;
    }
    ...without worrying about namespaces.

    However if you were to use the newer header, you would need to worry about the namespace issue in one of the three previously defined ways:
    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello world!" << std::endl;
        return 0;
    }
    The purpose of namespaces are to avoid conficts with other objects that could potentially be named the same thing as something you're attempting to build or use (a third party library for example). This is really more of a concern with larger projects.

    [edit]Some of this might duplicate some of what grumpy said.[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 07-20-2007, 01:07 PM
  2. What is wrong with my code? My first program......
    By coreyt1111 in forum C++ Programming
    Replies: 11
    Last Post: 11-14-2006, 02:03 PM
  3. Replies: 7
    Last Post: 08-06-2004, 09:14 AM
  4. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM