Thread: FAQ: Header files, include statements

  1. #1
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401

    FAQ: Header files, include statements

    i dont completely understand how compilers process different header files, and also .cpp files.

    im trying to formulate a system whereby i can add a class to my program that consists of a .h file and a .cpp file. These files contain completely independent code from the main program that can be added into any program, not based on or derived from any other classes or code. but i cant figure out how to include everything properly. the compiler always tells me this function is already defined or that function isn't defined yet; i have to fiddle around with the include statements a lot to make it work, and it never makes sense.

    what is the most efficient way to do this?

    for example, say main.cpp and main.h contain code that displays a window, adds child windows, etc. Then i have a class, benClass, that is contained within bClass.cpp and bClass.h. where do i add the includes so that everything functions properly?

    and also, changing things around a little, if i have a function in main.h that i want to call from bClass.cpp, where do i include everything? thanks for any advice.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    this is how you should split a class over .h and .cpp files and then be able to use in another cpp file...

    Code:
    Point.h
    
    #include<iosfwd>
    
    class Point
    {
       public:
         Point();
         Point(int,int);
         ~Point();
         int GetX()const;
         int GetY()const;
         void SetXY(int,int);
         friend std::ostream& operator << (std::ostream&,const Point&);
    
       private:
         int X;
         int Y;
    };
    Code:
    Point.cpp
    
    #include "Point.h"
    #include<iostream>
    
    Point::Point() : X(0),Y(0) {}
    Point::Point(int x,int y) : X(x),Y(y) {}
    Point::~Point() {}
    int Point::GetX()const { return X;}
    int Point::GetY()const {return Y;}
    void Point::SetXY(int x,int y) { X=x; Y=y;}
    std::ostream& operator << (std::ostream& os,const Point& p)
    {
         os<<"( "<<p.X<<","<<p.Y<<" )";
         return os;
    }
    Code:
    Main.cpp
    
    #include "Point.h"
    #include <iostream>
    
    int main()
    {
        Point p(10,10);
        std::cout <<p<<std::endl;
        p.SetXY(20,20);
        std::cout<<p<<std::endl;
        return 0;
    }
    Not windows specific but will give you the idea.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    suddenly it works. though i suspect things may get more complicated as my code advances, so expect more posts! thanks a lot.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where to include header files?
    By kaos_frack in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2008, 03:09 PM
  2. MFC include BS
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 10-31-2005, 12:44 PM
  3. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM
  4. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  5. more header files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-28-2001, 01:56 PM