Very Simple Question. Need Help. Hello,world

#include

int main(){
cout<<"hello"<<endl;
}

What is wrong with this. When I wrote programs I used the header iostream.h. I try to compile this in Dev-C++ this is what it says below, but if i use the old header iostream.h it complains about how old it is but it will work, same on unix.


In function `int main()':
`cout' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
`endl' undeclared (first use this function)

Comments

  • : #include
    :
    : int main(){
    : cout<<"hello"<<endl;
    : }
    :
    : What is wrong with this. When I wrote programs I used the header iostream.h. I try to compile this in Dev-C++ this is what it says below, but if i use the old header iostream.h it complains about how old it is but it will work, same on unix.
    :
    :
    : In function `int main()':
    : `cout' undeclared (first use this function)
    : (Each undeclared identifier is reported only once for each function it appears in.)
    : `endl' undeclared (first use this function)
    :
    [blue]
    Someone else will have to supply the details as to why, but the new iostream has some changes that make it necessary to either add the following line:

    [code]
    using namespace std;
    [/code]

    prior to main(), or add std:: to your functions:

    [code]
    std::cout<<"hello"<<std::endl;
    [/code]

    Take Care,
    Ed[/blue]



  • : Someone else will have to supply the details as to why (...)

    It is because namespaces where introduced in the last version of the C++ standard. Think of namespace as java packages, they can contain large sets of objects in a semantical way. This has many bennefits, which include the difficulty for name collision.

    On C++ all standard library objects and functions are on a special namespace the [b]standard[/b] (hence the name [b]std[/b]) namespace. So to use any standard function you have to declare it's using namespace std.

    Note that using the statement

    [/code]
    using namespace std;
    [/code]

    at the beginning of the file isn't recommended since it will import every function on the std namespace to your namespace. Instead use long name for standard objects, or declare wich functions or objects of the std namespace you want to use. I. E.:

    [code]
    std::cout << "Hello World!" << std::endl; // prefferable

    [/code]

    or

    [code]
    using std::cout;
    using std::endl;

    cout << "Hello World!" << endl;
    [/code]

    For more information on namespaces, I advice you to read the article in the following link: http://www.cprogramming.com/tutorial/namespaces.html

    Homero C. de Almeida

    [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]

  • So everytime i want to use a cout/cin statement or delcare a string i should use std:: becase i notice string doesn't work without using namespace std;

    : : Someone else will have to supply the details as to why (...)
    :
    : It is because namespaces where introduced in the last version of the C++ standard. Think of namespace as java packages, they can contain large sets of objects in a semantical way. This has many bennefits, which include the difficulty for name collision.
    :
    : On C++ all standard library objects and functions are on a special namespace the [b]standard[/b] (hence the name [b]std[/b]) namespace. So to use any standard function you have to declare it's using namespace std.
    :
    : Note that using the statement
    :
    : [/code]
    : using namespace std;
    : [/code]
    :
    : at the beginning of the file isn't recommended since it will import every function on the std namespace to your namespace. Instead use long name for standard objects, or declare wich functions or objects of the std namespace you want to use. I. E.:
    :
    : [code]
    : std::cout << "Hello World!" << std::endl; // prefferable
    :
    : [/code]
    :
    : or
    :
    : [code]
    : using std::cout;
    : using std::endl;
    :
    : cout << "Hello World!" << endl;
    : [/code]
    :
    : For more information on namespaces, I advice you to read the article in the following link: http://www.cprogramming.com/tutorial/namespaces.html
    :
    : Homero C. de Almeida
    :
    : [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]
    :
    :

  • [b][red]This message was edited by Vilanye at 2006-1-29 17:34:22[/red][/b][hr]

    You CAN do it everytime, as was noted before, just do this before main()

    using std::cout;
    using std::cin;
    ect

    I don't think there is any benefit to actually putting the std:: statements inside the code, everytime you use a std lib function, but it does add in a lot more typing.

    If you know Java, think of using as analogous to the import statements. Without the import statements in java you would have to do stuff like java.net.ServerSocket sock;

    : So everytime i want to use a cout/cin statement or delcare a string i should use std:: becase i notice string doesn't work without using namespace std;
    :
    : : : Someone else will have to supply the details as to why (...)
    : :
    : : It is because namespaces where introduced in the last version of the C++ standard. Think of namespace as java packages, they can contain large sets of objects in a semantical way. This has many bennefits, which include the difficulty for name collision.
    : :
    : : On C++ all standard library objects and functions are on a special namespace the [b]standard[/b] (hence the name [b]std[/b]) namespace. So to use any standard function you have to declare it's using namespace std.
    : :
    : : Note that using the statement
    : :
    : : [/code]
    : : using namespace std;
    : : [/code]
    : :
    : : at the beginning of the file isn't recommended since it will import every function on the std namespace to your namespace. Instead use long name for standard objects, or declare wich functions or objects of the std namespace you want to use. I. E.:
    : :
    : : [code]
    : : std::cout << "Hello World!" << std::endl; // prefferable
    : :
    : : [/code]
    : :
    : : or
    : :
    : : [code]
    : : using std::cout;
    : : using std::endl;
    : :
    : : cout << "Hello World!" << endl;
    : : [/code]
    : :
    : : For more information on namespaces, I advice you to read the article in the following link: http://www.cprogramming.com/tutorial/namespaces.html
    : :
    : : Homero C. de Almeida
    : :
    : : [italic]There's no dishonour in failure. For we aren't allowed to know wheter we'll achieve success or not. There is only one final shame, the cowardice of not trying.[/italic]
    : :
    : :
    :
    :

    [italic][blue]Just my 2 bits[/blue][/italic]



Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories