0

I am trying to understand a few examples of pragma and header compilation with include guards

I am not asking about the difference between them. I am asking specifically if based on the example I provided that I am following, am I illustrating it correct in my class?

I can not tell if its working or not when I run my program.

In the examples they show this,

#pragma once 
#if !defined(x_header_included) 
#define x_header_included 
class X { … }; 
#endif 

Which in my c++ ignorance I have translated to this,

#include <iostream>

#pragma once
#if !defined(my_headers)
#define my_headers

#include "npc.h"
#include "pc.h"
#include "health.h"

class game {
private:
    npc n;
    pc p;
    health h;

public:
    game(const npc& init_n, const pc& init_p, const health& init_h):
    n(init_n),
    p(init_p),
    h(init_h)
    {}


    game(std::string gen, std::string abil, bool use, int lvl, int h, int arm) :
    n(gen, abil),
    p(use, lvl),
    h(h, arm)
    {
    }

    friend std::ostream& operator<<(std::ostream& s, const game& g) {
        g.n.output(s);
        g.p.output(s);
        g.h.output(s);
        return s;
    }

    npc get_n() { return n; }
    pc get_p() { return p; }
    health get_h() { return h; }

    void set_n(npc init_n) { n = init_n; }
    void set_p(pc init_p) { p = init_p ; }
    void set_h(health init_h) { h = init_h; }
};

#endif

Is that all I do or am I missing something? Does anything else need to be added to the .cpp files from here? The class I am showing is the composite top level class included in my main.cpp and my game.cpp file.

wuno
  • 8,388
  • 15
  • 72
  • 163
  • 1
    these are called include guards; read more about them f.e. here [C++ #include guards](http://stackoverflow.com/questions/8020113/c-include-guards) – wonko realtime Sep 12 '14 at 16:16
  • If `#pragma once` substituted that comprehensive `game` class definition in place of `class X { ... }` then it doesn't seem very general purpose. Maybe that's just confined to one compiler vendor? ;) – Brian Cain Sep 12 '14 at 16:17
  • possible duplicate of [#pragma once vs include guards?](http://stackoverflow.com/questions/1143936/pragma-once-vs-include-guards) – Brian Cain Sep 12 '14 at 16:18
  • @BrianCain #pragma once is microsoft specific afaik and just a shortcut for the include guards - read more [here](http://msdn.microsoft.com/en-us/library/4141z1cx.aspx) – wonko realtime Sep 12 '14 at 16:18
  • 1
    @wonkorealtime: `#pragma once` is not MS-specific, but it's certainly not portable. – Paul R Sep 12 '14 at 16:20
  • 1
    @wonkorealtime: `#pragma once` is supported by gcc and clang too. – Jarod42 Sep 12 '14 at 16:20
  • 2
    Other compilers support that pragma, too, but it's true: Any `#pragma` is by definition compiler-specific. The only thing in common is that compilers should ignore pragma directives they don't know. That said, include guards like any macro should be ALL_UPPERCASE in order to make them stick out from the other code. – Ulrich Eckhardt Sep 12 '14 at 16:22
  • Thank you for telling me other places to go read about this besides the 20 places I have already been. Would anyone mind answering my question though? – wuno Sep 12 '14 at 16:29

0 Answers0