![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |
|
Professional Programmer
|
.H and .CPP files
Lets say i have a header named Experience.h and another one named fight.h, they are both compiled in the same project.dev file, so if i have an integer in the experience.h called int exp;.. would the fight.h "interact" with the experience.h file? heres an example as best i can come up with without having tried it yet:
This is the fight.h: void fight()
{
cout << "you killed the monster.";
if (player_1.health > frog.health){
exp = exp + 15;
else if (player_1.health < frog.health) {
cout << "You died.";
}
}Experience.h: void experience()
{
int exp = 0;
if (exp == 400){
cout << "You have gained a level!";
}
}So what is happening is that the exp int is being created in the experience.h, but i need to know if it will also interact with other .h and .cpp files, since it is in the same project. If they do interact, i also would like to know if the int would be carried over to other .cpp's, like.. if int exp gained 20 in the main.cpp, if i printed the int from the experience.h would it have printed 20 aswell or would it be different values? If this doesnt make sense, just diregard it im very tired and will probably understand what im trying to do in the morning. EDIT: I forgot to add, would i have to create all the variables i want to be used across all the .cpp and headers as a global variable in the main .cpp?
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
I'm not much of a C++ guy but I believe what you've got there is merely a local variable and i think what you are looking for is a global variable.
since they are seperate functions they will not carry over to each other unless you pass them by value, pass them by reference, or make them global.
__________________
I have never let my schooling interfere with my education. -Mark Twain- Xbox live gamertag: melbolt |
|
|
|
|
|
#3 | |
|
Professional Programmer
|
Alright, so global would be before the int main() function in the main.cpp right? If so then this thread is done.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
You should not define your functions in header files. Header files are for declarations and maybe some extern variables.
Read some tutorials on it, short one: here.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Please listen to nnxion. Header files are designed to convey information to multiple source files (by including them in more than one source file). Consequently, if they have definitions, one will get multiple definitions. Header files are pasted into the source file at the point of inclusion. There is nothing to be gained by having a header used in just one source file, except organizational divisibility. When the compiler sees it, the header and the source will be one thing.
To divide your source, put it in multiple files. Things that need to be known to more than one source file are declared an a header (or more than one header). That header is then included in each source file that needs the declarations. The compiler is converting statements to machine code. It isn't running the program. I needs to know what variables and functions look like, it doesn't need to use them. At the end of a compilation or set of compilations one will have object files which need to refer to each other, usually. The linker links them together. At that point, things must actually exist somewhere, rather than just be prototyped.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 | |
|
Professional Programmer
|
Im not going by this thread anymore, i already found out that what i was trying to do is next to completely useless.. It just means more work than i have to do so im not even going to bother, i found an equally as good way of doing it, that takes not so much effort to figure out either.. its in my project thread if you care to see what it looks like now
![]()
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
I hope you take the opportunity to check out the information provided in this "dead" thread after it died without warning. Misperceptions can complicate and ruin your code. The average member may or may not jump around trying to keep up, and may or may not move his or her germane replies about.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|