C Board  

Go Back   C Board > General Programming Boards > FAQ Board

 
 
LinkBack Thread Tools Display Modes
Old 12-28-2002, 08:49 AM   #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.
__________________
benforbes@optusnet.com.au
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro

Code Tags
Programming FAQ
Tutorials
bennyandthejets is offline  
Old 12-28-2002, 09:04 AM   #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
Stoned_Coder is offline  
Old 12-28-2002, 09:10 AM   #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.
__________________
benforbes@optusnet.com.au
Microsoft Visual Studio .NET 2003 Enterprise Architect
Windows XP Pro

Code Tags
Programming FAQ
Tutorials
bennyandthejets is offline  
 

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
where to include header files? kaos_frack C++ Programming 6 12-30-2008 03:09 PM
MFC include BS Bubba Windows Programming 4 10-31-2005 12:44 PM
header and source files gtriarhos C Programming 3 10-02-2005 03:16 AM
Headers that use each other nickname_changed C++ Programming 7 10-03-2003 04:25 AM
more header files Unregistered C++ Programming 2 10-28-2001 01:56 PM


All times are GMT -6. The time now is 11:55 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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