'cout' undeclared indentifier?

This is a discussion on 'cout' undeclared indentifier? within the C++ Programming forums, part of the General Programming Boards category; in one of my .cpp classes the prog doesn't realize that i have include iostream and VC++ gives me errors. ...

  1. #1
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572

    'cout' undeclared indentifier?

    in one of my .cpp classes the prog doesn't realize that i have include iostream and VC++ gives me errors. Here is what I have:

    Code:
    //===================MAIN.CPP
    #include <iostream>
    #include <cstdlib>
    #include "gridworld.h"
    using namespace std;
    
    int main()
    {
    
    	CGridworld test1(5, 5);
    //...AND SO ON
    
    //=====================gridworld.h
    // File:  gridworld.h
    
    #ifndef _GRIDWORLD_H
    #define _GRIDWORLD_H
    
    #include "DLList.h"
    #include "personarray.h"
    
    class CGridworld
    {
      
    
      public:
    
        CGridworld(int nrows=5, int ncols=5);//constructor with gridsize
        //~CGridworld();
    
    	void printMe(int r=5);
    
        void move(int pid, int new_row, int new_col); //move person pid to 
    						  //  (new_row,new_col)
        void printMembers(int i, int j);	//print the people in district (i, j)
    
      private:
    
        int **Grid;	//two-dimentional array
        int nrows;			//number of rows in this grid world
        int ncols;			//number of columns in this grid world
        //CPersonArray P;		//stores person info
    };
    
    
    #endif
    
    #include "gridworld.cpp"
    
    
    //====================gridworld.cpp
    CGridworld::CGridworld( int nrows, int ncols)
    {
    	Grid = new int*[nrows];
    
    	for( int i = 0; i < nrows; i++)
    	{
    		Grid[i] = new int[ncols];
    	}
    
    }
    
    void CGridworld::printMe(int r)
    {
    	for(int j=0; j<r; j++){
    		cout << Grid[j] << endl;//the problem is here
    	}
    }
    so the problem lies in either gridworld.h or .cpp. I'm not sure how to do it exactly. In GCC it seems to work fine?!

    axon
    Last edited by axon; 09-21-2003 at 08:33 PM.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    try making it std::cout<<...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    Originally posted by JaWiB
    try making it std::cout<<...
    I did try it and this is the error i am getting:

    Code:
    \CS201F03\prog1\gridworld.cpp(19) : error C2653: 'std' : is not a class or namespace name
    \CS201F03\prog1\gridworld.cpp(19) : error C2065: 'cout' : undeclared identifier
    CS201F03\prog1\gridworld.cpp(19) : error C2297: '<<' : illegal, right operand has type 'int *'
    \CS201F03\prog1\gridworld.cpp(19) : error C2653: 'std' : is not a class or namespace name
    \CS201F03\prog1\gridworld.cpp(19) : error C2065: 'endl' : undeclared identifier
    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  4. #4
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Perhaps VC wants every .h specified in the file that use it, I mean... try to put <iostream> in gridworld.h
    Nothing more to tell about me...
    Happy day =)

  5. #5
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    I think you need to include your .h file in your .cpp file.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  6. #6
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    i've tried most everything i could think of, and nothing since to work.

    >>Perhaps VC wants every .h specified in the file that use it, I mean... try to put <iostream> in gridworld.h

    I know it could be done, but isn't this bad coding?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  7. #7
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    everything works fine if I include iostream in gridworld.cpp as but why?!?

    could this be a VC++ quirk?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  8. #8
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    I saw from your error log that that the errors located in gridworld.cpp. So this is the fix plus some bonus.
    1. add this on gridworld.cpp:
    #include "gridworld.h"
    #include <iostream>
    using std::cout;

    2. delete this on gridworld.h:
    #include "gridworld.cpp"

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by axon
    everything works fine if I include iostream in gridworld.cpp as but why?!?

    could this be a VC++ quirk?

    axon
    No, each .cpp file is compiled independantly of all others. Main.cpp is compiled without ever looking at girdworld.cpp. Gridworld.cpp is compiled without ever looking at main.cpp. Ever .cpp needs to #include anything it needs to use.
    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.

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Location
    Mt. Prospect, IL
    Posts
    2,572
    >>No, each .cpp file is compiled independantly of all others.
    >>Main.cpp is compiled without ever looking at girdworld.cpp.

    than why does the original code work on GCC?

    axon

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Perhaps have you asked GCC to compile only main.cpp? In that case, well, it works well because main.cpp includes girdworld.h which one includes girdworld.cpp. So the whole thing compiled as a single file. So I guess...

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Exactly what lyx said; if you compile ONLY main.cpp, it would work.

    BTW, a .cpp file should #include .h files, but a .h file should NEVER #include a .cpp, ever. That is terrible, terrible, terrible form. It IS good that you broke it up into pieces, but you are connecting the pieces wrong.
    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.

  13. #13
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    It depends on what is in your source file, because you may want to put inline functions into it; (some people do) so, in that case, you could include a .cpp file into a .h one.

  14. #14
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I think it's best to simply define the inline functions in the .h directly, and I think most people use this convention as well.

    That makes sense with how it is compiled, too -- if you included a .cpp, that .cpp itself should not be compiled; this is counterintuitive.
    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.

  15. #15
    lyx
    lyx is offline
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    Yes, and so do I but the fact is some people do put it into .cpp files and it's a choice, everyone's free to do as he likes as long as he tell us where are the things.

Page 1 of 2 12 LastLast
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. cout undeclared/cin undeclared
    By akbigchillin in forum C++ Programming
    Replies: 6
    Last Post: 04-24-2006, 04:06 PM
  4. Problem with cout
    By kristentx in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 12:37 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