Using header files and .cpp files efficiently in C++. A question.An example would be nice too. :)
16 Replies - 3227 Views - Last Post: 12 September 2010 - 12:53 PM
#1
Using header files and .cpp files efficiently in C++. A question.
Posted 11 September 2010 - 05:12 AM
I was unable to find a nice example about how to use header files and additional .cpp files. I was able to find only a few very minimalistic games/programs source code, which contains a lot of code, which i do not understand. By that i mean other libraries, sdks, etc.
This question was raised out of another forum's question of mine about functions and header files acting strangely. After that, i decided to investigate, if my logic was correct, as putting game logic into header files... It seems, that it wasn't.
I got a few answers, but I'm still a little bit confused about them. As of now, i think this:
I should put all the declarations into classes/functions on header files. Then i should create a main.cpp file, into which i should put... some code of some sort, not sure yet, which. Then, I should create two .cpp files containing game logic and menu logic... Mmm. But how would they interact? I mean... main.cpp has int main(), which should execute all the main code... So what is about those other .cpp files? How would the code be executed? Should i create functions in those .cpp files, as i did in header files earlier, and include these functions in the main.cpp file?
You see, a lot of questions raises. So I'm still a little bit confused about these things... And i was rewriting a game of my own with lots of new knowledge every day/2 days. Now i would like to get knowledge about these things, and rewrite it nicely, for the code to look nice, and be nice to read and edit too. Maybe I'll be able to complete the rewriting at last, lol.
Replies To: Using header files and .cpp files efficiently in C++. A question.
#2
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 11 September 2010 - 06:15 AM
#3
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 11 September 2010 - 10:36 AM
main.cpp
#include <iostream>
#include "declarations.h"
using namespace std;
int main()
{
cout << add(10, 20);
getchar();
return 0;
}
declarations.h
#ifndef DEC_H // if DEC_H is NOT defined, then we execute the relevent code. this prevents multiple inclusions #define DEC_H extern int add(int a, int B)/>; // add() is defined externally #endif /* DEC_H */
definitions.h
#include "declarations.h" // we need to include the declarations here, so we know what we're defining
int add(int a, int B)/>
{
return a + b;
}
This may not be a great example, but it should show you the basic methodology. I didn't include classes as i think you should really learn how to prevent mulitple inclusions and undefined external references first (i used the extern keyword to avoid this, you could also use static).
The final problem would be to link the files, this is the job of your compiler, i use visual studio, so simply adding the files to the project does this, as for other compilers, you will need to link them manually. for example, in G++.
g++ main.cpp definitions.cpp
sorry if this is a terrible explination, but to be honest, just play around with it and you will get it. When you fully understand how to create a program that can span multiple files, you will realise how powerful C and C++ is, and you will be able to make better use of the procedural and even OO nature of C++.
post any issues you have
This post has been edited by Aphex19: 11 September 2010 - 10:48 AM
#4
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 11 September 2010 - 11:00 AM
If header.cpp includes header.h, where header.h contains prototypes and header.cpp includes the definitions.
Then when header.h is included in main.cpp. The compiler intern looks for all files that includes header.h and includes them as well. So header.cpp is "implicitly" included for lack of a better term.
So say you have two files a.cpp and b.cpp that both include xyz.h
When you include xyz.h in main.cpp both a.cpp and b.cpp are again "implicitly" included.
main.cpp -----------------> xyz.h
| |
| |
a.cpp b.cpp
However anything defined in a.cpp and b.cpp must be prototyped in xyz.h
Example:
xyz.h
int add(int, int); int subtract(int, int);
a.cpp
int add(int a, int c)
{
return a + c;
}
b.cpp
int subtract(int a, int c)
{
return a - c;
}
// ERROR: divide is not defined if you try to use this function in main
int divide(int a, int c)
{
return a / c;
}
main.cpp
#include <iostream>
#include "xyz.h"
using namespace std;
int main()
{
cout << "add(1, 2) = " << add(1, 2) << endl;
cout << "subtract(1, 2) = " << subtract(1, 2) << endl;
// ERROR: DIVIDE IS NOT DEFINED
// cout << "divide(4, 2) = " << divide(4, 2) << endl;
}
This post has been edited by eker676: 11 September 2010 - 11:12 AM
#5
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 11 September 2010 - 12:08 PM
Quote
There is no implicit inclusion. Inclusion would also mean violation of one definition rule.
#6
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 07:48 AM
#7
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 07:58 AM
#8
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 08:17 AM
1>main.obj : error LNK2005: "public: __thiscall cDAT::~cDAT(void)" (??1cDAT@@QAE@XZ) already defined in game_logic.obj
1>main.obj : error LNK2005: "public: bool __thiscall cDAT::Create(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?Create@cDAT@@QAE_NV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@3@@Z) already defined in game_logic.obj
1>main.obj : error LNK2005: "bool __cdecl quit_true(bool)" (?quit_true@@YA_N_N@Z) already defined in game_logic.obj
1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library
Using Visual Studio 2010.
#9
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 08:28 AM
#10
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 08:43 AM
#pragma once #ifndef HEADER_H #define HEADER_H // code goes here #endif /* HEADER_H */
#11
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 08:55 AM
#ifndef GameLogic_h
#define GameLogic_h
bool quit_true(bool exit_failure)
{
exit_failure = false;
return exit_failure;
}
void GamePlaying();
#endif
I'm still getting the same errors. Tried with #pragma once too.
Code, if needed: http://pastebin.com/cFNRfkWG.
This post has been edited by JackOfAllTrades: 12 September 2010 - 09:46 AM
#12
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 09:19 AM
EDIT: baavgai sorted that out. I'll fix it in your post.
This post has been edited by JackOfAllTrades: 12 September 2010 - 09:46 AM
#13
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 09:37 AM
// GameLogic_h #ifndef GameLogic_h #define GameLogic_h bool quit_true(bool exit_failure); void GamePlaying(); #endif
Understand that headers are descriptions, definitions, and general sanity checks. All the headers in world won't put object code into your project. Conversely, if your header is simply forward declarations, you can actually get away without it; though that's considered bad form.
It's hard to diagnose these issues with just one element. One way for you to worry it out is to compile one file at a time. You should have one file with no dependence on other files. If all your files have cross decencies, it's time to reconsider your organization, maybe make another source file.
Compile another file that depends on one you've already successfully compiled and work your way up.
Good luck.
JackOfAllTrades, on 12 September 2010 - 10:19 AM, said:
Actually, he included a period
#14
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 09:51 AM
As for the purpose of the function, you can only write return EXIT_FAILURE; in the int main() function, and since I'm checking for failure to load .dat file, i need to do something on the exit. So I've done this in int main() function:
GamePlaying();
if(quit_true(true))
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
#15
Re: Using header files and .cpp files efficiently in C++. A question.
Posted 12 September 2010 - 10:35 AM
|
|

New Topic/Question
Reply




MultiQuote







|