1

I have a bit of a problem with header's and cpp files, I started creating a little text based game and i wanted to make all the work more clearer by creating more files and separated files to each class (Player.cpp, Monster.cpp Entity etc..).

So I ended up with a lot of #include directive's in each cpp file (i haven't used headers yet).

I needed a the rand function so i added stdlib to each class, and iostream to player class and etc..

so i ended up with a lot of double and triple uses of the same library, what should i do to avoid it? is there any way to avoid it at all? (creating a separate class for random for example wont work because i need to add it to the classes as well any ways).

Thanks in advance!

EDIT: But if i have something like this?:

main.cpp -> Player.h -> stdlib.h

     -> Monster.h -> stdlib.h

what can i do to make stdlib.h to be in all classes without warning or errors?

In other words, how can i create header's without including a lot of the same library in each with? (Player and Monster are different classes and they both need stdlib, what should i do to include just once that library?)

Naor Hadar
  • 537
  • 1
  • 6
  • 19
  • 1
    At first introduce headers using [include guards](http://en.wikipedia.org/wiki/Include_guard). Second you can nest your includes - e.g. write a (meaningful) header file e.g. Player.h including only the headers needed for the Player component. This way components using Player need to include only Player.h ...and not everything which is used for the implementation of Player which might change. – Jost Oct 11 '13 at 13:02
  • I am reading from a book called c++ for the impatient but they didnt teach my how to use it or told me to use it. I just wanted to make it more clearer for me. Is there any tutorial for this? – Naor Hadar Oct 11 '13 at 13:09
  • 1
    For books and guides have a look at the well known list [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) ...keep your chin up ;-) – Jost Oct 11 '13 at 13:34

3 Answers3

1

This may not be the best way but this is the way I do it I always have a main.cpp file which called a 'MasterHeader.h' file and inside that header file I have all the other headers, so in the main I only have one line which calls all my other headers and that header sorts out what headers I need.

So

main.cpp > MasterHeader.h > myHeader.h, myOtherHeader.h, thisHeader.h

Etc

  • This is also the method I use. Pros: organizing and including headers is dead-simple. Cons: unless your build system is really smart you need to recompile everything whenever a header changes. As long as your program isn't huge that last bit isn't so bad. – Max Oct 11 '13 at 13:27
1

What do you mean with "I haven't used headers yet"? You must create headers for you function declarations / class definitions. There is no other reasonable way.
Next you need to read about include guards. Include guards ensure that a header is not included more than once, directly and indirectly.
In almost all modern compilers you can use #pragma once instead of the old #if !defined... style.

cdoubleplusgood
  • 1,309
  • 1
  • 11
  • 12
  • 2
    It should be emphasized, however, that `#pragma once` is **not** standard C++; `#if !defined` is, and is widely used in new code, despite the disparaging adjective "old". – Pete Becker Oct 11 '13 at 13:16
  • Pete is correct. I just wanted to simplify things a little bit. – cdoubleplusgood Oct 11 '13 at 13:21
  • @PeteBecker It is best to use both, because some compilers don't recognize include guards and will fail to utilize certain optimizations related to header files. – Overv Oct 11 '13 at 13:30
  • @Overv - um, every compiler recognizes include guards; that's why they're the standard idiom for managing include files. Granted, some compilers don't go out of their way to avoid opening files when they've seen that a file has a normal-style include guard. For most people here this is completely irrelevant; beginners should be taught standard idioms, not implementation-specific optimizations of compile time (that sometimes have shaky semantics). – Pete Becker Oct 11 '13 at 13:38
  • @PeteBecker It appears I was wrong. I thought that most large C(++) projects used both to optimize compile times, but that does not appear to be the case. I stand corrected. – Overv Oct 11 '13 at 13:44
1

Correct me if i get this wrong... Your facing errors of redefinition due to including your own cpp files at multiple points.

One way would be to create a header for the code files you need to include elsewhere and declare your exports in this header. Ensure that the exports are defined once per created object by using the preprocessor

// some_file.h
#ifndef SOME_FILE_H
#define SOME_FILE_H
...
//export declarations
...
#endif

or you avoid including your code files and declare the functions you need as 'extern'

//your_functions.cpp
int some_function( int arg1 )
{
    ...
}

and

//other_file.cpp
extern int some_function( int );
Nicholas Feix
  • 154
  • 1
  • 5
  • I think your last code snippet is wrong. Why do you declare a function pointer? Also, for function declarations `extern` is not required. – cdoubleplusgood Oct 11 '13 at 13:11
  • Names that begin with an underscore followed by a capital letter (`_SOME_FILE_`) and names that contain two consecutive underscores are reserved to the implementation. Don't use them. – Pete Becker Oct 11 '13 at 13:17
  • oh crap, you are right. I had another thought where this is relevant but in this case you don't need to declare a pointer – Nicholas Feix Oct 11 '13 at 13:20
  • I doubt that there would be any conflicts with this name, but I accept your advice Pete and change it ;) – Nicholas Feix Oct 11 '13 at 13:27