Thread: header files, include statements

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

    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
    you bastard, 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

  4. #4
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753
    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.
    this is sort of irrelevant, but it sounds like you are trying to create an executable virus. an executable virus is one that is not a free-standing program, but adds itself (the code) onto other executables.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Header files - where to include
    By C+/- in forum C++ Programming
    Replies: 5
    Last Post: 01-13-2007, 10:45 AM
  2. Replies: 4
    Last Post: 12-14-2005, 02:21 PM
  3. Header Files
    By Volair in forum C Programming
    Replies: 2
    Last Post: 12-09-2005, 10:51 AM
  4. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM