Results 1 to 10 of 17

Thread: cout issue

Hybrid View

  1. #1
    Join Date
    Aug 2006
    Beans
    140

    cout issue

    I've decided to try and learn a programming language. I used C++ many moons ago at Uni and thought if I'm going to start somewhere might as well be there. So I've installed Eclipse along with the extra C++ files. I've have written the following code:

    Code:
    #include <iostream.h>
    #include "stdafix.h"
    
    int main()
    {
    	cout << "Hello World!\n";
    		return 0;
    }
    which I assumed would simply work. Unfortunatly I get the error '"Symbol 'cout' could not be resolved." - Resource hello.cpp, path /hello, line 12, Type semantic error'.
    I figured it would be something I had written wrong so I rang the C++ Hellow World example that comes in Eclipse but this also fails to run with the same error.

    Have I missed something somewhere ?

    Thanks.

  2. #2
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,082
    Distro
    Ubuntu Studio 13.04 Raring Ringtail

    Re: cout issue

    cout is in the std namespace. either you need to give the full address to it
    Code:
    std::cout << "Hello World!\n";
    or you can put
    Code:
    using namespace std
    after the includes, to tell the compile to search std for identifiers.

  3. #3
    Join Date
    Apr 2012
    Beans
    5,412

    Re: cout issue

    I think your confusion arises because <iostream.h> is non-standard (and deprecated - in fact I'm not even sure it's included in modern distributions) - iirc it predated the adoption of namespaces so plain 'cout' used to work back in the day

    You should probably be using <iostream> (without the .h) - in which case you will need to specify the namespace, as ssam says

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!\n";
        return 0;
    }
    See article here for more info / background --> http://members.gamedev.net/sicrane/a.../iostream.html

    EDIT: not sure what your "stdafix.h" is - maybe you are trying to mimic Visual Studio's "stdafx.h" convention?
    Last edited by steeldriver; November 21st, 2012 at 04:40 PM. Reason: added link to background article

  4. #4
    Join Date
    Aug 2006
    Beans
    140

    Re: cout issue

    Thank you both very much for your help. Seems it was further back when I used it than my brain cells first thought. That has worked perfectly for me thanks.
    As for the stdafix.h, I found it in a C++ ebook I was reading and it was suggested as part of the test code for Hello World so I wasn't sure if it was something I was missing. Needless to say I have not stuck with that book.

  5. #5
    Join Date
    Aug 2006
    Beans
    140

    Re: cout issue

    Quote Originally Posted by toontastic View Post
    Thank you both very much for your help. Seems it was further back when I used it than my brain cells first thought. That has worked perfectly for me thanks.
    As for the stdafix.h, I found it in a C++ ebook I was reading and it was suggested as part of the test code for Hello World so I wasn't sure if it was something I was missing. Needless to say I have not stuck with that book.
    Actually I was wrong, that hasn't worked either. Still getting an error about cout. If I try to build the code I get.

    Code:
    **** Build of configuration Debug for project Hello ****
    
    make all 
    Building file: ../hello.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"hello.d" -MT"hello.d" -o "hello.o" "../hello.cpp"
    ../hello.cpp: In function ‘int main()’:
    ../hello.cpp:6:2: error: ‘cout’ was not declared in this scope
    ../hello.cpp:6:2: note: suggested alternative:
    In file included from ../hello.cpp:1:0:
    /usr/include/c++/4.7/iostream:62:18: note:   ‘std::cout’
    What I now have written is

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

  6. #6
    Join Date
    Apr 2012
    Beans
    39

    Re: cout issue

    There is nothing wrong with that code. It should just compile and run. There must be something else going wrong.

  7. #7
    Join Date
    Apr 2012
    Beans
    5,412

    Re: cout issue

    Agreed - it builds and runs for me [NB on gcc/g++ 4.6.3 not 4.7] even with all of your extra compile options

    How did you install the build tools? maybe something is misconfigured?

    EDIT: just tried it in a crunchbang (Debian) VM with gcc/g++ 4.7.1 and it works there as well
    Last edited by steeldriver; November 23rd, 2012 at 12:30 AM. Reason: confirmed OK on gcc/g++ 4.7

  8. #8
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: cout issue

    Quote Originally Posted by toontastic View Post
    Actually I was wrong, that hasn't worked either. Still getting an error about cout. If I try to build the code I get.

    Code:
    **** Build of configuration Debug for project Hello ****
    
    make all 
    Building file: ../hello.cpp
    Invoking: GCC C++ Compiler
    g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"hello.d" -MT"hello.d" -o "hello.o" "../hello.cpp"
    ../hello.cpp: In function ‘int main()’:
    ../hello.cpp:6:2: error: ‘cout’ was not declared in this scope
    ../hello.cpp:6:2: note: suggested alternative:
    In file included from ../hello.cpp:1:0:
    /usr/include/c++/4.7/iostream:62:18: note:   ‘std::cout’
    What I now have written is

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Hello World!\n";
        return 0;
    }
    Are you sure that you pasted the correct contents of "hello.cpp"? Your compilation error message says that "cout" was not declared in line 6, but line 6 is actually "return 0;". Is it possible that in fact your IDE tries to compile your old code?

  9. #9
    Join Date
    Aug 2006
    Beans
    140

    Re: cout issue

    Quote Originally Posted by Zugzwang View Post
    Are you sure that you pasted the correct contents of "hello.cpp"? Your compilation error message says that "cout" was not declared in line 6, but line 6 is actually "return 0;". Is it possible that in fact your IDE tries to compile your old code?
    Hmmmm not sure, I'll try again deleting everything and creating a new project and let you know.

Bookmarks

Posting Permissions

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