• Create Account

### #Actuale‍dd

Posted 05 December 2012 - 06:59 AM

It's always a good idea to show the actual compiler errors. The whole point of them is that they're supposed to tell you (us) what's wrong!

It would also be a good idea to simmer down the code to a minimal compilable example. For example, I don't know if you've omitted header guards/"#pragma once" for brevity or if they really don't exist.

So I'm going to work on these assumptions:
• You have appropriate header guards, or are using "#pragma once"
• I'll assume the compiler error is something along the lines of "C++ forbids declaration of 'PlayerClass' with no type".
Ok, note how you have circular inclusion: playerclass.h includes weaponclass.h includes playerclass.h ...
Recall also that #include is somewhat like copy-and-pasting a header at the point of inclusion.

So after the preprocessor has done its work, we end up with something like this:

#ifndef WEAPONCLASS_H_
#define WEAPONCLASS_H_

// Filename: weaponclass.h

// --- begin #included playerclass.h ---
#ifndef PLAYERCLASS_H_
#define PLAYERCLASS_H_
// Filename: playerclass.h.

// --- begin #included weaponclass.h ---
#ifndef WEAPONCLASS_H_
// Circular inclusion has no effect.
#endif // WEAPONCLASS_H_
// --- end #included weaponclass.h ---

class PlayerClass
{
// blah blah blah
private:
WeaponClass *weapon;
};

#endif // PLAYERCLASS_H_
// --- end #included playerclass.h ---

class WeaponClass
{
// blah blah blah
private:
PlayerClass *weapon;
};

#endif // WEAPONCLASS_H_


Note how in the resulting translation unit, we've attempted to use the symbol "WeaponClass" as the type of a member of PlayerClass, even though "WeaponClass" hasn't been declared anywhere, thanks to the header guards and circular inclusion. Because there's no declaration, the compiler doesn't know what "WeaponClass" is, or how to treat it.

The way around this is to use forward declarations rather than inclusion. For example:

#ifndef PLAYERCLASS_H_
#define PLAYERCLASS_H_
// Filename: playerclass.h

class WeaponClass;

class PlayerClass
{
//blah blah blah
private:
WeaponClass *weapon;
};

#endif // PLAYERCLASS_H_


Then do the same thing in weaponclass.h. In your .cpp files you can #include both headers. I'll leave you to figure out why that works (just follow the copy-and-paste the preprocesor does and remember that it's valid to have multiple declarations that aren't definitions).

EDIT: and perhaps somewhat off topic, but having a weapon that knows about the player sounds a bit fishy to me.

### #1e‍dd

Posted 05 December 2012 - 06:56 AM

It's always a good idea to show the actual compiler errors. The whole point of them is that they're supposed to tell you (us) what's wrong!

It would also be a good idea to simmer down the code to a minimal compilable example. For example, I don't know if you've omitted header guards/"#pragma once" for brevity or if they really don't exist.

So I'm going to work on these assumptions:
• You have appropriate header guards, or are using "#pragma once"
• I'll assume the compiler error is something along the lines of "C++ forbids declaration of 'PlayerClass' with no type".
Ok, note how you have circular inclusion: playerclass.h includes weaponclass.h includes playerclass.h ...
Recall also that #include is somewhat like copy-and-pasting a header at the point of inclusion.

So after the preprocessor has done its work, we end up with something like this:

#ifndef WEAPONCLASS_H_
#define WEAPONCLASS_H_

// Filename: weaponclass.h

// --- begin #included playerclass.h ---
#ifndef PLAYERCLASS_H_
#define PLAYERCLASS_H_
// Filename: playerclass.h.

// --- begin #included weaponclass.h ---
#ifndef WEAPONCLASS_H_
// Circular inclusion has no effect.
#endif // WEAPONCLASS_H_
// --- end #included weaponclass.h ---

class PlayerClass
{
// blah blah blah
private:
WeaponClass *weapon;
};

#endif // PLAYERCLASS_H_
// --- end #included playerclass.h ---

class WeaponClass
{
// blah blah blah
private:
PlayerClass *weapon;
};

#endif // WEAPONCLASS_H_


Note how in the resulting translation unit, we've attempted to use the symbol "WeaponClass" as the type of a member of PlayerClass, even though "WeaponClass" hasn't been declared anywhere, thanks to the header guards and circular inclusion. Because there's no declaration, the compiler doesn't know what "WeaponClass" is, or how to treat it.

The way around this is to use forward declarations rather than inclusion. For example:

#ifndef PLAYERCLASS_H_
#define PLAYERCLASS_H_
// Filename: playerclass.h

class WeaponClass;

class PlayerClass
{
//blah blah blah
private:
WeaponClass *weapon;
};

#endif // PLAYERCLASS_H_


Then do the same thing in weaponclass.h. In your .cpp files you can #include both headers. I'll leave you to figure out why that works (just follow the copy-and-paste the preprocesor does and remember that it's valid to have multiple declarations that aren't definitions).

PARTNERS