Thread: "hello! world" gives error

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    29

    Arrow "hello! world" gives error

    I am new to both C++ as well as Linux.

    The following program is to print "hello! world"
    When I complie it saying
    c++ file1.cpp
    The follwoing errors occur... what could be the reason?
    -----------------------------------------------
    [ashesh@ecl5 learnc]$ c++ file1.cpp
    In file included from /usr/include/c++/3.2/backward/iostream.h:31,
    from file1.cpp:2:
    /usr/include/c++/3.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using
    one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
    [ashesh@ecl5 learnc]$
    -------------------------------------------
    C.B.Ashesh,
    Hyderabad, India

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    465
    try using #include <iostream> rather than #include <iostream.h>

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    use of #include <iostream> resulted in the following errors...

    [ashesh@ecl5 learnc]$ c++ file1.cpp
    file1.cpp: In function `int main()':
    file1.cpp:6: `cout' undeclared (first use this function)
    file1.cpp:6: (Each undeclared identifier is reported only once for each
    function it appears in.)
    [ashesh@ecl5 learnc]$
    C.B.Ashesh,
    Hyderabad, India

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <iostream>
    using namespace std; //Not the best solution,but o.k if you know what you are doing.

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    29

    seems to be compiler problem

    I have checked running the "hello!world" program on some other linux systsm. It worked fine.

    So I think there seems to be some compiler problem... AndI think that is what the error is trying to say... some /usr/include/c++/3.2/iostream.... etc


    But I cannot understand much of this. So can someone please let me know how I can uninstal the present compiler and install a new one... and from where can I get a c++ compiler which willwork on Linux Redhat 8.0
    C.B.Ashesh,
    Hyderabad, India

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    29
    hi Wledge

    yes, the program works when I use
    using namepsace std

    But I dont know why I am using it and what it does...

    The program I wrote without namespace is working on one compiler but doesn't work on the present on...

    Good that I tried Wledge code otehrwise, I would have ended up deletingmy compiler and searching for a new one...
    C.B.Ashesh,
    Hyderabad, India

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    The error message was because <iostream.h> is an old header file, which is only there so you can compile old programs. You should always use <iostream>, which wraps all of the functions from <iostream.h> in the std namespace.

    So, in fact, there is nothing wrong with your compiler, it was just adhering to the standards.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by ashesh
    hi Wledge

    yes, the program works when I use
    using namepsace std

    But I dont know why I am using it and what it does...
    cout and endl and other provided functions are now provided in the std namespace. To access cout you must say std::cout. To access endl you must say std::endl
    Code:
    using namespace std;
    makes the std:: automatic so you don't have to use it all the time. If you wanted, you could also say:
    Code:
    using std::endl;
    using std::cout;

  9. #9
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by XSquared
    The error message was because <iostream.h> is an old header file, which is only there so you can compile old programs. You should always use <iostream>, which wraps all of the functions from <iostream.h> in the std namespace.

    So, in fact, there is nothing wrong with your compiler, it was just adhering to the standards.
    As an interesting aside, Visual Studio .NET 2003, which I just installed, doesn't even SUPPLY iostream.h anymore, nor any of the deprecated C++ libraries.

    And ashesh, your compiler isn't broken, the code was. Since the newest C++ standard in 1998, <iostream.h> has been superceded, and the compiler was informing you that you should use the new (and correct) headers.

  10. #10
    Registered User deleeuw's Avatar
    Join Date
    Aug 2001
    Posts
    31
    Ashesh, just curious, what other includes do you have besides
    <iostream>?

  11. #11
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Reply

    Yes there are!

    Somehow the compiler needs the required include stuff in order to succesfully compile lines of code.

    But I am also new at this so I can't answer your question more precise than this. Maybe some1 else can..

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Unhappy Reply

    #include <string.h>
    #include <stdlib.h>
    #include ....

    eh, that's all I know

  13. #13
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    <stdlib.h> etc are depreciated. Instead, take off the .h and put a c in front of it. Thus <stdio.h> becomes <cstdio>. For a list of all standard include files, try here.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  14. #14
    Registered User
    Join Date
    Jun 2003
    Posts
    73
    this is the program for Hello World. :-)



    #include <iostream.h>
    int main()
    {
    cout<<"Hello World!" <<endl;
    return 0;
    }

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Except that program hasn't been truly legal C++ since 1998.

    In 1998, they redid the standard library. That code, romeoz, MIGHT compile, on compilers that support an older standard, but on modern compilers, it should give, at the very least, a warning. On some newer ones, it won't compile at all. That code is no longer correct C++.

    The correct program is:

    Code:
    #include <iostream>
    
    using namespace std; // There are better ways than this, but it's acceptable
    
    int main()
    {
        cout<<"Hello World!" <<endl;
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM