Header Class Woes

Started by
1 comment, last by Rob Loach 20 years, 5 months ago
I''ve struggled with this for quite a bit of time now. I''m trying to put an instance of a class inside a different class in a different header file. But whenever I put the #include header files in the way I think it theoretically should be, it doesn''t work. I always get the same errors. Anyone know where the correct places to put the #include "x.h"s? main.cpp

#include "rA1Main.h"

rA1Main Engine;

int main (void)
{
    //Engine.Initialize("Program!!!", true, 640, 480);

    /*
    Error: .WaitUntilKeyPress must have class/struct/union type.
    But it is already part of Keyboard, which is part of Engine...
    */
    Engine.Keyboard.WaitUntilKeyPressed();

    return 0;
}
rA1Main.h

#ifndef _RA1MAIN_H_
#define _RA1MAIN_H_
#pragma once

class rA1Main
{
public:
    rA1Keyboard Keyboard;
};

#endif /* _RA1MAIN_H_ */
rA1Main.cpp

#include <allegro.h>
#include "rA1Keyboard.h"
#include "rA1Main.h"
rA1Keyboard.h

#ifndef _RA1KEYBOARD_H_
#define _RA1KEYBOARD_H_
#pragma once

class rA1Keyboard
{
public:
      void WaitUntilKeyboard(void);
};

#endif /* _RA1KEYBOARD_H_ */
rA1Keyboard.cpp

#include "rA1Keyboard.h"
void rA1Keyboard::WaitUntilKeyPress(void)
{
    // code
    return;
}
Rob Loach
Website: Over-Development
Current Project: Pong, The First

"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed."
- The Boondock Saints
Rob Loach [Website] [Projects] [Contact]
Advertisement
You need to (or, in some cases, merely should ) include all files referenced by a particular file. In other words, because rA1Main.h uses rA1Keyboard, you need to include that file.

#ifndef _RA1MAIN_H_#define _RA1MAIN_H_//do not use "pragma once" if you use the ifndef thing; it's redundant#include "rA1Keyboard.h"class rA1Main{public:    rA1Keyboard Keyboard;};#endif /* _RA1MAIN_H_ */


Also, POST YOUR ERRORS. "an error" is useless; I'm just guessing that your error was a file wasn't included. Visual C++ would say "undefined something" because rA1Keyboard wasn't defined for rA1Main.h.

EDIT: Looking further at your code, I can tell it is indeed a header that wasn't included. Compiling rA1Main.cpp, you include Keyboard.h before you include Main.h, so that will compile fine even though Main.h didn't include Keyboard.h -- it was included before it was compiled, so that's fine. However, compiling main.cpp does make an error because it only includes "main.h" without including keyboard.h.

~CGameProgrammer( );

-- Post screenshots of your projects. 100+ posts already in the archives.

[edited by - CGameProgrammer on November 16, 2003 9:53:25 PM]
~CGameProgrammer( );Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Great. Got it working. Thanks a lot CGameProgrammer .
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement