Multiple header files , cout undelcared probem

This is a discussion on Multiple header files , cout undelcared probem within the C++ Programming forums, part of the General Programming Boards category; main.cpp Code: #include <iostream> #include "hdr.h" using namespace std; int main() { cin.get(); return 0; } hdr.h Code: #ifndef HDR_H ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    29

    Multiple header files , cout undelcared probem

    main.cpp
    Code:
    #include <iostream>
    #include "hdr.h"
    using namespace std;
    
    
    int main()
    {
    	
    
    	
    	cin.get();
    	return 0;
    }
    hdr.h
    Code:
    #ifndef HDR_H  
    #define HDR_H
    
    class point
    {
    public:
    	point();   
    	void print(void);
    private:
    	int x;
    	int y;
    };
    #endif
    hdr.cpp
    Code:
    #include "hdr.h"
    
    point::point()
    {
    	y = 0;
    	x = 1;
    }
    
    void point::print(void)
    {
    	//cout <<"TEST"; // cout is undeclared why?
    }
    These files are in an empty devcpp project. Why can't i use cout in the hdr.cpp file? Thankyou for your time.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    You must #include <iostream> in hdr.cpp also...

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    I'm not the final word on this, but I'm pretty sure headers must be declared in all .cpp source files in order to use them. If you included iostream in hdr.h, then it would be a different story, but you included it only in main.cpp. I'm also pretty sure that you need to declare what namespace you're using in all files.
    Sent from my iPadŽ

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Do you also need to put using namespace std in all of the files or just one?
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    29
    Thank you, I had previously put in #include <iosteam>, but forgot the using namespace std;

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I'm not the final word on this, but I'm pretty sure headers must be declared in all .cpp source files in order to use them
    Just to qualify that a bit: you only need to include the header files that you need in a file. Which header files do you need in a file? If some code in a file uses the line:
    Code:
    cout<<someVar;
    then you have to include <iostream> in that file. You also need a using declaration in that file because 'cout' is being used without the std:: qualifier.

    The guiding priciple is that you need to declare all the names you use in a file. The compiler does not like to be startled by guests that haven't been introduced previously.
    Last edited by 7stud; 11-22-2005 at 09:07 PM.

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by 7stud
    ...the header files that you need in a file.
    Yes, that's what I meant.

    Thanks for clarifying, though, I didn't realize how vague I was.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  2. Replies: 4
    Last Post: 12-14-2005, 01:21 PM
  3. Using c++ standards
    By subdene in forum C++ Programming
    Replies: 4
    Last Post: 06-06-2002, 09:15 AM
  4. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

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