DiggyJohn
DiggyJohn

Reputation: 424

Getting errors when using forward declarations of a class in Xcode

I'm having an issue trying to use the forward declaration of one of my classes in Xcode.

Here's my basic architecture:

MyClassA.h:

class MyClassA{
  ... list of members and method prototypes ...
};

MyClassA.cpp:

#include "MyClassA.h"

--- list of methods for MyClassA ...

MyClassB.h:

class MyClassA; // forward declaration

class MyClassB{
  ... list of members and methods ...
  MyClassA* ptrToA;
};

MyClassB.cpp:

#include "MyClassB.h"

... list of methods for MyClassB ...

This works on my windows machine in Visual Studio no problem, do it all the time. However, on the Mac in Xcode it's giving me the error:

Forward declaration of 'struct MyClassA'

Any ideas? The error message doesn't seem to make much sense since I declared class plain as day and it's coming back with an error about a struct. Are forward declarations not supported in Xcode (pretty simple error message seems to indicate this is the issue, other than the struct/class confusion)? My project is compiled as C++, not C. Is there another project setting I'm missing that could be causing this?

Thanks.

Upvotes: 1

Views: 4827

Answers (2)

Rob Napier
Rob Napier

Reputation: 299605

What line of code is actually causing the error? My expectation is that you have forgotten to include MyClassA.h in MyClassB.cpp, and the error is actually happening when you try to new the object.

Classes are structs. They just mark their members private by default, while structs mark them public.

Upvotes: 0

wilhelmtell
wilhelmtell

Reputation: 58715

Make sure you have #include "MyClassA.h" in MyClassB.cpp, unless you don't intend to dereference ptrToA, or otherwise need to know anything about the underlying type.

Upvotes: 1

Related Questions