Using header files and .cpp files efficiently in C++. A question.

An example would be nice too. :)

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 6260 Views - Last Post: 12 September 2010 - 12:53 PM Rate Topic: -----

#1 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 119
  • Joined: 22-April 09

Using header files and .cpp files efficiently in C++. A question.

Posted 11 September 2010 - 05:12 AM

Hi everyone.
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.

Is This A Good Question/Topic? 0
  • +

Replies To: Using header files and .cpp files efficiently in C++. A question.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 11 September 2010 - 06:15 AM

Have you read this?
Was This Post Helpful? 0
  • +
  • -

#3 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 11 September 2010 - 10:36 AM

Basically, you create header files with the correct multiple inclusion guards (decleration file), then you create the cpp file (definitions). When you wish to use the function and what not that are contained within the cpp file, you include the appropriate header file. Take this for an example.

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

Was This Post Helpful? 0
  • +
  • -

#4 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 11 September 2010 - 11:00 AM

The way I came to understand the process was sort of a reverse inclusion.

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

Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 11 September 2010 - 12:08 PM

Quote

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.
False.

There is no implicit inclusion. Inclusion would also mean violation of one definition rule.
Was This Post Helpful? 0
  • +
  • -

#6 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 119
  • Joined: 22-April 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 07:48 AM

Thanks for explanations. But I'm having trouble trying it on my project. Here's my code of three files... Please don't copy it. I would like to ask, why is it not working: http://pastebin.com/J8gg7pnQ/.
Was This Post Helpful? 0
  • +
  • -

#7 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 07:58 AM

Define not working.
Was This Post Helpful? 0
  • +
  • -

#8 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 119
  • Joined: 22-April 09

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)" (??0cDAT@@QAE@XZ) already defined in game_logic.obj
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.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 08:28 AM

Header include guards
Was This Post Helpful? 0
  • +
  • -

#10 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 08:43 AM

As you're using Visual Studio, you can also use #pragma once as an inclusion guard. This is what i do for extra security

#pragma once
#ifndef HEADER_H
#define HEADER_H

// code goes here

#endif /* HEADER_H */

Was This Post Helpful? 0
  • +
  • -

#11 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 119
  • Joined: 22-April 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 08:55 AM

Okay, now this is how my header of GameLogic.h looks like:

#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

Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6063
  • View blog
  • Posts: 23,515
  • Joined: 23-August 08

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 09:19 AM

Can we talk about that function there? What is its purpose? All it's doing is ignoring the input variable, setting it to false and returning it (false).

And your latest pastebin expired.

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

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 09:37 AM

When they say "code" in the head, they're talking about appropriate code in header.

// 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.

View PostJackOfAllTrades, on 12 September 2010 - 10:19 AM, said:

And your latest pastebin expired.


Actually, he included a period :P http://pastebin.com/cFNRfkWG
Was This Post Helpful? 0
  • +
  • -

#14 newn  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 119
  • Joined: 22-April 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 09:51 AM

Well, the code works alright, if i don't put the GamePlaying() function into game_logic.cpp, but copy the code into int main() function. However, if i put the code into a function into one more .cpp file, it doesn't work. Hm.

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;

Was This Post Helpful? 0
  • +
  • -

#15 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Using header files and .cpp files efficiently in C++. A question.

Posted 12 September 2010 - 10:35 AM

Try declaring the functions as extern in your header file, basically telling the compiler than the main definition is contained within another file, perfect. If you want to avoid all these problems with unresolved externals, you might want to use classes, which don't seem to suffer from the same problems. In short though, extern is a brilliant keyword, use it :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2