iDevGames Forums
c++ forward declaration question - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: c++ forward declaration question (/thread-3355.html)



c++ forward declaration question - gradyeightythree - Apr 14, 2007 12:18 PM

Hello all -

I'm new to the board/mac game programming in general and I have a question regarding forward declarations in c++ with xcode (gcc 4.0). I've been working on developing an engine based off of the one built in the book Game Code Complete. My problem is as follows:

In Actor.h:
struct ActorParams
{
// definition here
};

In Interface.h:
class IGame
{
// definitions
public:
virtual void VAddActor(IActor* actor, struct ActorParams* ap) = 0;
};

This kind of forward declaration worked fine with Visual C++, but XCode gives me a 'forward declaration of 'struct ActorParams' error. Is there any way around this short of including actor.h/moving the definition of ActorParams to Interface.h?


c++ forward declaration question - martinsm - Apr 14, 2007 01:16 PM

In Interface.h you must write:
Code:
struct ActorParams;

class IGame
{
    // definitions
public:
    virtual void VAddActor(IActor* actor, struct ActorParams* ap) = 0;
};



c++ forward declaration question - gradyeightythree - Apr 14, 2007 03:22 PM

I thought that was what I was doing by saying "struct" in front of ActorParams in the VAddVector function?

Regardless, adding what you said gives me the same forward declaration error.


c++ forward declaration question - martinsm - Apr 14, 2007 03:33 PM

No, saying struct before struct type is old C syntax.
In C++ between struct and class there is no differences except one - default visibility for class is private, but for struct public.
So in any place you use struct keyword, it can be simply skipped (except for declaration, of course).

Is error shown on that virtual void VAddActor line? Maybe try skipping struct keyword.

Strange that this code works (compiles) for me with gcc 3.4
Code:
struct ActorParams;
void f(struct ActorParams* a);



c++ forward declaration question - OneSadCookie - Apr 14, 2007 03:34 PM

Tell us the exact error, and the exact line it points to, and show us what's included from where, what namespaces are in effect, etc.


c++ forward declaration question - gradyeightythree - Apr 14, 2007 03:47 PM

Alright -- there are two errors:

BacGame.cpp:44: error: invalid use of undefined type 'struct ActorParams'
BacInterfaces.h:66: error: forward declaration of 'struct ActorParams'

The first error refers to the use of the ActorParams pointer from the a VAddActor() definition for a derived class of IGame -- BacGame. It looks like this:
ap->m_Aid = m_LastActorId;

This is in a different file -- BacGame.cpp -- which has a class that inherits from the IGame interface. The second error is pointing at the virtual void VAddActor line that was mentioned in my last post. Taking out the struct in that function doesn't fix it.

Includes--

main.cpp
- #include "BacStd.h"
- #include "BacGame.h"
- #include "BacActor.h"

BacStd.h
-#include <iostream>
-#include <list>
-#include <map>
-#include <string>
-#include "BacInterfaces.h"

BacGame.cpp
- "BacStd.h"
- "BacGame.h"

BacActor.h and BacInterface.h don't include anything.

If there is something I'm forgetting, let me know.


c++ forward declaration question - OneSadCookie - Apr 14, 2007 03:59 PM

martinsm's first post seems like it should resolve the second error. The first just needs a #include of Actor.h.


c++ forward declaration question - gradyeightythree - Apr 14, 2007 04:12 PM

Blah -- you're absolutely right. Including Actor.h fixed both errors, thanks much!