FedoraForum.org - Fedora Support Forums and Community
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2011
    Location
    Amritsar
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Unexpected errors in C++ program?

    Here is the program code:

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

    Errors using g++ in terminal :

    [Raman@Fedora CPP]$ g++ hello.cpp
    hello.cpp: In function ‘int main()’:
    hello.cpp:3:1: error: ‘cout’ was not declared in this scope

    This is first time I am using g++.I am not able to remove these errors .plz help
    Laptop:
    Operating System:Fedora 14, Kernel Linux 2.6.35.6-45.fc14x86_64, Gnome 2.32.0
    Memory: 2 GiB
    Processor: Pentium(R) Dual-Core CPU T4500

  2. #2
    Join Date
    Mar 2009
    Location
    Lancaster, UK
    Posts
    932
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    C++ uses namespaces (Have a quick google)

    The error is correct, 'cout' has not been declared. The function is std::cout. If you put this in it will work.

    You can avoid typing std:: all the time by putting:

    Code:
    using namespace std;
    after your #includes.

    Then it will work.


    Just to make sure it is clear. It should be:


    Code:
    #include<iostream>
    int main() {
      std::cout<<"Hello World";
    return 0;
    }
    or

    Code:
    #include<iostream>
    using namespace std;
    int main() {
      cout<<"Hello World";
    return 0;
    }

  3. #3
    Join Date
    Feb 2011
    Location
    Amritsar
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Re: Unexpected errors in C++ program?

    Thanks , It has successfully compiled the file and a new file named a.out has been created.
    How to run this?
    Laptop:
    Operating System:Fedora 14, Kernel Linux 2.6.35.6-45.fc14x86_64, Gnome 2.32.0
    Memory: 2 GiB
    Processor: Pentium(R) Dual-Core CPU T4500

  4. #4
    Join Date
    Nov 2010
    Location
    Tallahassee, FL
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    1) you'll probably want to rename it to something else, other than a.out
    a) mv a.out newname

    2) chmod +x newname

    3) ./newname

  5. #5
    Join Date
    Feb 2011
    Location
    Amritsar
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    a) mv a.out newname //changes the name of a.out

    2) chmod +x newname //changes the mode of file to executable

    3) ./newname //executes the file

    I think i have understood everything right...Thanks
    Laptop:
    Operating System:Fedora 14, Kernel Linux 2.6.35.6-45.fc14x86_64, Gnome 2.32.0
    Memory: 2 GiB
    Processor: Pentium(R) Dual-Core CPU T4500

  6. #6
    Join Date
    Mar 2009
    Location
    Lancaster, UK
    Posts
    932
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    Quote Originally Posted by Raman009
    Thanks , It has successfully compiled the file and a new file named a.out has been created.
    How to run this?
    You should find that it is executable by default so you should not need to follow the steps that briand gave.

    If I were you I would specify the name of the output file when you compile it. See below:

    Code:
    g++ hello.cpp -o hello.exe
    ./hello.exe
    That will run your program and you should see

    Code:
    Hello World[Raman@Fedora CPP]$
    If you want a new line after "Hello World" change

    Code:
    std::cout<<"Hello World";
    to

    Code:
    std::cout<<"Hello World" << std::endl;
    If you have not got a book on C++ I would recommend: "Sams Teach Yourself C++ in One Hour a Day" I found it excellent.

  7. #7
    Join Date
    Feb 2011
    Location
    Amritsar
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    Quote Originally Posted by Adunaic
    Code:
    g++ hello.cpp -o hello.exe
    ./hello.exe
    If you have not got a book on C++ I would recommend: "Sams Teach Yourself C++ in One Hour a Day" I found it excellent.
    Thanks ,this method is quicker than the previous one..
    Laptop:
    Operating System:Fedora 14, Kernel Linux 2.6.35.6-45.fc14x86_64, Gnome 2.32.0
    Memory: 2 GiB
    Processor: Pentium(R) Dual-Core CPU T4500

  8. #8
    Join Date
    Nov 2004
    Location
    MT USA
    Posts
    1,971
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    You could have ran the application: ./a.out

    Also in linux you don't need to '.exe' . That's a Windows thing.

    $g++ hello.cpp -o hello

    ...

    $ ./hello

  9. #9
    Join Date
    Feb 2011
    Location
    Amritsar
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Unexpected errors in C++ program?

    Thanks for replying
    Laptop:
    Operating System:Fedora 14, Kernel Linux 2.6.35.6-45.fc14x86_64, Gnome 2.32.0
    Memory: 2 GiB
    Processor: Pentium(R) Dual-Core CPU T4500

Similar Threads

  1. NIC gets unexpected device name
    By ratcrow in forum Hardware
    Replies: 0
    Last Post: 9th September 2006, 06:32 PM
  2. VNC - Unexpected Drop
    By t3hl33td4rg0n in forum Using Fedora
    Replies: 11
    Last Post: 18th May 2006, 10:40 PM
  3. ssh and unexpected DNS queries
    By jcliburn in forum Security and Privacy
    Replies: 3
    Last Post: 29th October 2005, 12:35 AM
  4. Unexpected LVT Crash
    By thelight in forum Using Fedora
    Replies: 4
    Last Post: 13th August 2004, 01:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •