Thread: Supernoob problem!

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    4

    Supernoob problem!

    Okay, so I'm using g++ and am getting an error that cout is not defined even when I include iostream:

    Code:
    #include <iostream>
    
    int main() {
    	cout << "This should show up!" << endl; 
    	return 0;
    }
    Error:
    Code:
    testclr.cpp: In function `int main()':
    testclr.cpp:2: error: `cout' undeclared (first use this function)
    testclr.cpp:2: error: (Each undeclared identifier is reported only once for
       each function it appears in.)
    testclr.cpp:2: error: `endl' undeclared (first use this function)

  2. #2
    Rad gcn_zelda's Avatar
    Join Date
    Mar 2003
    Posts
    942
    You've correctly included the 'iostream' header file, but the function 'cout' is part of a namespace within iostream called the std namespace.

    After
    Code:
    #include <iostream>
    , you have to put
    Code:
    using namespace std;
    .

    Or, when you use 'cout', put this instead :
    Code:
    std::cout
    . That tells the compiler which namespace you're using.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    Yeah, what he said.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        cout<<"This should show up!!!";
        return 0;
    }
    or

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout<<"This should show up!!!";
        return 0;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Option #3

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    int main() {
        cout << "This should show up!" << endl; 
        return 0;
    }
    "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. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM