在 Xcode 中使用类的前向声明时出现错误
我在尝试在 Xcode 中使用我的类之一的前向声明时遇到问题。
这是我的基本架构:
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 ...
这在我的 Windows 机器上的 Visual Studio 中工作没有问题,一直这样做。然而,在 Mac 上的 Xcode 上它给了我错误:
Forward declaration of 'struct MyClassA'
有什么想法吗?错误消息似乎没有多大意义,因为我将 class plain as day 声明为 day,并且它返回了有关结构的错误。 Xcode 中是否不支持前向声明(非常简单的错误消息似乎表明这是问题所在,除了结构/类混淆之外)?我的项目被编译为 C++,而不是 C。我是否缺少另一个可能导致此问题的项目设置?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您有
#include "MyClassA.h"
在 MyClassB.cpp 中,除非您不打算取消引用
ptrToA
,或者需要了解有关基础类型的任何信息。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.哪一行代码实际上导致了错误?我的期望是您忘记将 MyClassA.h 包含在 MyClassB.cpp 中,并且当您尝试
new
对象时实际上发生了错误。类是结构。默认情况下,它们只是将其成员标记为私有,而结构将它们标记为公共。
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.