![]() |
| | #1 |
| mustang benny Join Date: Jul 2002
Posts: 1,401
| FAQ: Header files, include statements 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 |
| | #2 |
| Skunkmeister 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;
}
__________________ 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 |
| | #3 |
| mustang benny 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 | |
|
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 |