Thread: how do i create a header file?

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    494

    how do i create a header file?

    can anyone explain to me the steps in creating header file. the book talks about header files, how to include them and what they do, but it doesnt say how to create 1, like the step by step process. do i create it the same way i create a .cpp file? but with .hpp extension?
    When no one helps you out. Call google();

  2. #2
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    1. Create a text file named whatever.h (or .hpp)
    2. You're done, what a genius you are now!
    Do not make direct eye contact with me.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    lol, does it have to b in the same directory as the .cpp file? i guess so right?
    When no one helps you out. Call google();

  4. #4
    The Defective GRAPE Lurker's Avatar
    Join Date
    Feb 2003
    Posts
    949
    Not necessarily, you can do something like this:

    #include "myfolder\myheader.h"

    or if it's in a folder above the current one:

    #include "..\myheader.h"
    Do not make direct eye contact with me.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    im having a problem... when i compile the prog, i get this
    Code:
    c:\program files\microsoft visual studio\myprojects\aprogram\rect.cpp(40) : fatal error C1075: end of file found before the left brace '{' at 'c:\program files\microsoft visual studio\myprojects\aprogram\rect.hpp(21)' was matched
    any ideas?
    When no one helps you out. Call google();

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Your braces aren't matched.

    Since you are using Visual C++, just stick the cursor next to a curly brace '{' or '}' and hit Ctrl-] which will take you to the matching brace (if there is one). Do that until you find a brace that matches up incorrectly (or not at all).

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    in the Rect.hpp thers 2 classes with matching braces. in the rect.cpp theres 3 pairs of matching braces and when i click on the error it sends me at the brace at the end of the file after return 0; .. i dont get it
    When no one helps you out. Call google();

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Chance are your files are small enough to post if there are only 5 pairs of braces, so post your code.

    Don't forget to put a semi-colon after class declarations (class Rect { ... };). Also, make sure you have include guards (check the FAQ on header files).

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    #include <iostream>
    class Point  //holds x, y coordinates
    {
    	//no constructor, use default
    public:
    	void SetX(int x){itsX =x;}
    	void SetY(int y){itsY =y;}
    	int GetX() const {return itsX;}
    	int GetY()const {return itsY;
    private:
    	int itsX;
    	int itsY;
    };//end of point class declaration
    
    class Rectangle
    {
    	public: 
    			Rectangle (int top, int left, int bottom, int right);
    			~Rectangle(){}
    
    			int GetTop()const {return itsTop;{
    			int GetLeft()const {return itsLeft;}
    			int GetBottom()const{return itsBottom;}
    			int getRight() const {return itsRight;}
    
    			Point GetUpperLeft() const {return itsUpperLeft;}
    			Point GetLowerLeft() const {return itsLowerLeft;}
    			Point GetUpperRight() const {return itsUpperRight;}
    			Point GetLowerRight() const {return itsLowerRight;}
    
    			void SetUpperLeft(Point Location) {itsUpperLeft = Location;}
    			void SetLowerLeft(Point Location) {itsLowerLeft = Location;}
    			void SetUpperRight(Point Location) {itsUpperRight = Location;}
    			void SetLowerRight(Point Location) {itsLowerRight = Location;}
    
    
    			void SetTop(int top) { itsTop =top;}
    			void SetLeft(int left) {itsLeft =left;}
    			void SetBottom (int bottom) { itsBottom = bottom;}
    			void SetRight (int right) {itsRight = right;}
    
    			int GetArea()	const;
    	private:
    			Point itsUpperLeft;
    			Point itsUpperRight;
    			Point itsLowerLeft;
    			Point itsLowerRight;
    			int	  itsTop;
    			int   itsLeft;
    			int   itsBottom;
    			int   itsRight;
    };
    			//end rect.hpp
    rect.cpp
    Code:
    #include "Rect.hpp"
    Rectangle::Rectangle(int top, int left, int bottom, int right)
    {
    	itsTop =top;
    	itsLeft = left;
    	itsBottom = bottom;
    	itsRight = right;
    
    	itsUpperLeft.SetX(left);
    	itsUpperLeft.SetY(top);
    
    	itsUpperRight.SetX(right);
    	itsUpperRight.SetY(top);
    
    	itsLowerLeft.SetX(left);
    	itsLowerleft.SetY(bottom);
    }
    
    //compute area of the rectangle by finding cornersides,
    //establish width and height and then multiply
    
    int Rectangle::GetArea() const
    {
    
    	int Width = itsRight - itsLeft;
    	int Height = itsTop - itsBottom;
    	return(Width * Height);
    }
    int main()
    {
    	//intitialize a local Rectangle variable
    	Rectangle MyRectangle (100,20,50,80);
    
    	int Area = MyRectangle.GetArea();
    	std::cout <<"Area: "<<Area <<"\n";
    	std::cout <<"Upper Left X coordinate: ";
    	std::cout <<MyRectangle.GetUpperLeft().GetX();
    	return 0;
    }
    When no one helps you out. Call google();

  10. #10
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    found it lol
    Code:
    int GetY()const {return itsY;
    damn
    When no one helps you out. Call google();

  11. #11
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Code:
    int GetTop()const {return itsTop;{
    Check that also.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    yep, thanks ehehe
    When no one helps you out. Call google();

  13. #13
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    just a suggestion
    Code:
    private:
    	Point itsUpperLeft;
    	Point itsUpperRight;
    	Point itsLowerLeft;
    	Point itsLowerRight;
    	int   itsTop;
    	int   itsLeft;
    	int   itsBottom;
    	int   itsRight;
    change all those to upperLeft, upperRight, so on.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  14. #14
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You should also use so called Inclusion Guards in your header files. If you (accidently or not) include the header file twice you will get multiple definition errors.

    Point.h:
    Code:
    #ifndef SOME_UNIQUE_NAME
    #define SOME_UNIQUE_NAME
    
    ... //Stuff
    
    #endif
    Even if the file is included multiple times, the stuff between #ifndef/#endif will only be included once.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  15. #15
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    thanks for the heads up, i really just started learning about headers i dont think im in a position to do complicated stuff that will require a lot of header files. but i got a question for u Magos, how did u write that War3 SLK editor. id like to do something like that with a prog i got in the "to do list" after i get a bit comfortable and familiar with c++. any special topics in order to write something like that?

    edit: btw forgot to say that ur name = magician in greek. eheh
    When no one helps you out. Call google();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. When to create a header file?
    By jamie85 in forum C Programming
    Replies: 2
    Last Post: 03-20-2005, 11:57 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM