c++ compilation bug

Started by
9 comments, last by EvilNando 12 years, 11 months ago
First, you must realise that the preprocessor is simply glorified copy and paste. When it sees #include, it preprocesses the given file and pastes the source into the including file.

Consider what happens when ModuleVideo.cpp includes ModuleVideo.h
[source]
module video.h:
> #define MODULE_VIDEO_H_
> #include Common.h
Common.h:
> #define COMMON_H_
> #include standard headers
> #include Application.h
Application.h:
> #define APPLICATION_H_
> #include Common.h
common.h:
> COMMON_H_ already defined, do nothing
> class Application {
> // ...
> ModuleVideo module_video;

// And so on

[/source]
We can see that the preprocessed file (the "translation unit" the compiler actually sees) appears to start with the standard header file definitions, and then the Application class. The Application class references the (as yet undeclared) ModuleVideo class.

This is mostly because C++ continues to use a compilation model designed for 1970s computers.

Other points:

  • You don't want to use a "union" for the resolution member. Find out what unions mean, then use a struct or a class.
  • Consider using an enumeration for your error codes
  • Likewise use an enum or const integers for your DEFAULT WIDTH/HEIGHT
  • You should include C++ headers such as <cstdio> rather than <stdio.h>
  • Aggregating header files like "Common.h" should either:

    • Include your own header files but never be included from them
    • Exclude your own header files but may be included from them (e.g. it might reference the standard headers and any libraries you are using)

  • In the latter case you could consider using precompiled headers to speed up your compilation times.
  • Advertisement
    Thanks that helped

    So many years working with C# has made me a noob again! D:

    This topic is closed to new replies.

    Advertisement