Advertisement

Multiple Source Files

Started by December 23, 2000 10:01 PM
6 comments, last by PSioNiC 23 years, 10 months ago
hey i have a question not really OpenGL specific. i look at everyone elses project and they all have a bunch of source files and a header for each one. In my project I need same header on each file. I dont know what to include and in what files. I know you have to do something with #ifndef and #define to make sure everything doesn''t get defined twice. I tried including the header in each file, but I got 1,000,000 redefinition errors. anybody who has done this tell me how you did it thanks
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
Do you have variables in your headers? If so make the veriable in the cpp file and put an extern to it in your header.

InFerN0

Not all who wander are lost...
InFerN0Not all who wander are lost...
Advertisement
what i want is basically in VB that general declarations section

where is that in C++?
or what is the C++ equivalent to that?
"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
At the top of each header file, do this:

#ifndef _YOURFILE_H_
#define _YOURFILE_H_
And at the bottom:
#endif

This tells the compile to only compile it once so you don't get redefinition errors. Ofcourse you can replace _YOURFILE_H_ with whatever the hell you want.

For functions, all you need in the header is the prototype.
e.g:

void MyFunc(int Something);

Defines, typedefs, structs etc are all normal.
But variables must be declared as "extern" in the header like so:

extern int MyVariable;

This tells the compiler that the variable doesn't exist in the header, it exists somewhere else.
Where it exists is up to you, but you should usually put it in the corresponding .cpp file.

e.g:

#ifndef _MYHEADER_H_
#define _MYHEADER_H_

extern int MyVar;

#endif

Source file:

#include "myheader.h"
int MyVar;

void main() ..... etc

See?

Edited by - Quantum on December 23, 2000 11:35:07 PM
i understand that but my problem is different from what you are explaining

it is too hard for me to fully explain what is going on i will just figure it out myself
ok wait
say you have a source file called Source1.cpp, and a header called Header.h for that file. in that Header.h you have a class called MYCLASS. There is an instance of MYCLASS called mClass in Source1.cpp. you also have another source file called Source2.cpp

now you want to access the mClass''s properties and methods from Source2.cpp

what do you do ?

(sorry for the trouble)


"This is stupid. I can't believe this! Ok, this time, there really IS a bug in the compiler."... 20 mins pass ..."I'M AN IDIOT!!!"
Advertisement
Well, mClass is a variable. In Header.h if you do something like this:

class MyClass
{
public:
int Something;
};

extern MyClass mClass;

then in Source1.cpp:

MyClass mClass;

and in source2.cpp:

#include "header.h"

You should have access to it!
There are several ways to get your hands on class variables and functions. I would suggest reading up a bit on encapsulation and inheritance. If you are trying to access public members of another class in your second source file you might declare an instance of the first class in your second source file.

class FirstClass
{
public:
MyClass();
int x;
int y;

private:
int z;
};

class SecondClass
{
public:
SecondClass();
FirstClass fclass; //This is composition.

private:
float x;
};

Now SecondClass contains an instance of FirstClass called fclass.
You can get your hands on any public variables or functions through dot notation.

You could also declare the second class as a derived class of the first like this:

class Point3D :public Point // Point3D is now a derived class of
{ // class Point.
public:
Point3D();
Point3D(int x, int y, int z);
void SetZ(int z);

private:
int z;
};

This is an example of inheritance and your second class (Point) in this case will [inherit] all the public properties and behavior of the first class in addition to whatever functions and variables you add to the second class. You will still only have access to public members of the first class but private members can be accessed throught public functions.

Mostly it just depends on what you are trying to do, and it is up to you to decide which is the best approach. In other words, don''t use inheritance when simple composition will do. And visa versa.

Now that I have you thorougly confused I bid you farewell!
Seriously though, if you want to use C++ effectivly I suggest reading up on composition and inheritance, I think you will find your answer there.

Keep the dirty side down...
Sparky
Robert Snyder

This topic is closed to new replies.

Advertisement