c++ forward declaration question

Apprentice
Posts: 11
Joined: 2007.04
Post: #1
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?
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2006.10
Post: #2
In Interface.h you must write:
Code:
struct ActorParams;

class IGame
{
    // definitions
public:
    virtual void VAddActor(IActor* actor, struct ActorParams* ap) = 0;
};
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.04
Post: #3
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.
Quote this message in a reply
Apprentice
Posts: 13
Joined: 2006.10
Post: #4
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);
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
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.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.04
Post: #6
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
martinsm's first post seems like it should resolve the second error. The first just needs a #include of Actor.h.
Quote this message in a reply
Apprentice
Posts: 11
Joined: 2007.04
Post: #8
Blah -- you're absolutely right. Including Actor.h fixed both errors, thanks much!
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  "forward declaration" compiling error muby99 1 2,757 Oct 7, 2007 12:56 PM
Last Post: akb825