cout undeclared?

This is a discussion on cout undeclared? within the C++ Programming forums, part of the General Programming Boards category; Hi guys I used to program C, but recently took the plunge and decided to de-rust myself and start on ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    3

    cout undeclared?

    Hi guys

    I used to program C, but recently took the plunge and decided to de-rust myself and start on Cpp ... however I'm being driven mad by a "cout undeclared" error when I compile and I just can't see why. Apologies if this is a silly question but I can't see in help files where I'm going wrong.

    My project is as follows:

    - main.cpp file, includes cstdlib, iostream, string. Has calls to cout and works fine
    - class.h file, includes files as main.cpp. class and member function declarations
    - class.cpp file, includes all the same files again. member function definitions

    Now, although both the main and class files include the same files the main file manages to output with cout fine, but when I use cout in a member function in the class.cpp file it gives me a "cout undeclared" compiler error (and a "endl undeclared", but I guess if I fix one I fix the other).

    In the tutorials I'm working through they do the same thing but it doesn't seem to work for me. I'm using Dev C++ from Bloodshed Software if that's any help ..

    Thanks for anything
    eth

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Did you remember to prefix with std::? E.g. std::cout, std::endl, etc?

    Or you could use the uglier way of "using namespace std;" in both CPP files.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    Ah, namespace is it, fantastic. Not used to namespaces yet, thanks loads for that. I did think it might be something simple and silly I was missing!

    Fast response too!

    Cheers :)

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Just for reference, try to get into the habbit of not using

    Code:
    using namespace std
    and either delcare cout and other objects either one of two ways

    version 1 like cat suggested

    Code:
    std::cout << "My name is halo" << std::endl;
    Version 2, declare all namespaces before you use them

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    I use the second version, but some prefer the first as suggested by cat. It really doesn't matter really, but getting out of using namespace std declarations at an early stage is a big leap into good programming practice

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    3
    I assume that if I use the second method I can do:
    Code:
    using std::cout;
    using std::endl;
    
    cout << "Stuff" << endl;
    Also, whats wrong with namespace std declarations?

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Quote Originally Posted by ethorad
    Also, whats wrong with namespace std declarations?
    I enjoyed this thread on the subject a while back:

    including namespaces

  7. #7
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by ethorad
    Also, whats wrong with namespace std declarations?
    It teaches you bad programming style. When you get to more advanced projects, you'll often work with code in half a dozen or more namespaces, and keeping the namespace identifiers in place really makes the code far more clear. That's one reason I dislike almost all uses of "using", they often destroy entirely the benefits that namespaces give in improving readability of code.

    Also, for classes, it makes it easier to figure out which class you're working with, for example:

    string s;

    I don't know it's actually a std::string variable -- it could be a different class or typedef named string at global scope, or a class/typedef named string in a different namespace that's been imported with 'using'. Looking at just that line of code, I could make a guess as to what type the variable is but it's impossible to know for sure.

    Now, when I see:

    std::string s;

    I know exactly the type of the variable I'm dealing with.

    Being as the standard namespace has dozens if not hundreds of items with very common names, naming conflicts WILL arise if you don't take care to stamp them out early, and the best way to do that is to make sure you fully qualify types and functions with their namespace.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 PM
  4. 'cout' undeclared indentifier?
    By axon in forum C++ Programming
    Replies: 15
    Last Post: 09-22-2003, 03:48 PM
  5. I'm REALLY confused. Why isn't cout or cin working here?
    By niudago in forum C++ Programming
    Replies: 8
    Last Post: 02-15-2003, 04:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21