在类中声明对象时,什么可能导致此错误?
我正在努力完成这项作业:)
我有两个课程:海洋和网格。
当我声明海洋中的网格对象时:
unsigned int sharkCount;
Grid grid;
编译器/抱怨者说:
error C2146: syntax error : missing ';' before identifier 'grid'
你能用我提供的有限信息预测是什么产生了这个错误吗?
看来 Ocean 不喜欢 Grid 类。这可能是因为网格类的实现不佳造成的。顺便说一句,网格有一个默认的构造函数。
然而编译时却出现了错误!
编辑:它们各自位于单独的头文件中,并且我已将 Grid.h 包含在 Ocean.h 中。
I'm battling with this assignment :)
I've got two classes: Ocean and Grid.
When I declare an object of the Grid inside the Ocean:
unsigned int sharkCount;
Grid grid;
The compiler/complainer says:
error C2146: syntax error : missing ';' before identifier 'grid'
Can you possibly predict what produces this error with the limited info I provided?
It seems that as if the Ocean doesn't like the Grid class. Could this be because of the poor implementation of the grid class. BTW the Grid has a default constructor.
Yet the error happens in compiling time!.
EDIT: They're each in separate header file, and I've included the Grid.h in the Ocean.h.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的第一个猜测是,当您尝试在
Ocean
中使用Grid
时,它的定义根本不可见。通常,如果您将每个文件都放在自己的文件中,并且没有使用标头来允许每个文件被另一个文件“看到”,则通常会发生这种情况。My first guess would be that the definition of
Grid
simply isn't visible at the point that you've tried to use it inOcean
. Typically this happens if you have each in its own file, and haven't used a header to allow each to be "seen" by the other.我们需要其余的源代码,但有几个可能的答案:
We need the rest of the source, but there are a couple of possible answers:
为了使 Grid 在您的其他类中使用,您需要在标头中包含 Grid 的标头,或者对类进行前向声明并将其声明为类中的指针,然后在 .cpp 文件中实例化它。
或者最好是:
In order for Grid to be used in your other class you either need to include the header of the Grid in the header or do a forward declaration of the class and declare it as a pointer in your class then instantiate it in your .cpp file.
or preferably :