VS C++ 2010 Express 问题(IDE/头文件见鬼?)
我几乎写完了基于实体/组件的游戏逻辑引擎的初稿。它并不完全基于这一理念,但我也认为实施方式因人而异。我正在使用 OOP、STL 和虚函数用 C++ 进行编写。是的,我知道虚拟函数可能很慢,但它不是一种快速完成某些事情的练习,只是一个可以轻松扩展且易于被其他人理解的逻辑引擎(所有内容也都已注释)。如果您愿意这么称呼的话,这只是游戏开发新手的“软件设计”练习。
我的初稿已基本完成,核心服务(各种 inits、directX 等)已与游戏逻辑分离并可以使用。我只是想在制作我的第一个原型游戏之前完成逻辑引擎(我想到的是等距 ARPG)。 GL 引擎的构建就像一个类树,从顶部的基本抽象类“Entity”开始,并从中派生出越来越多的具体类。出于良好的设计原因,任何地方的继承级别都不会超过 3 或 4 级。我认为。
但我可能有一个严重的问题:在某些类中,我将其声明为另一个类的成员变量对象,会发生这种情况:在 IDE 启动并分析我编写的内容后,它会强调那些特定成员变量的类型(类名)并且它说它没有在任何地方声明。然后过了一会儿,红色下划线消失了,它说没问题,并声明(oO)每次分析代码时都会再次发生。这种情况也会发生在具有另一种类型的班级的另一个班级中。需要明确的是,我确实包含了所有相关的标头,它应该知道类型的声明以及它在哪里完成。
另外,如果我尝试编译,它会给我 gl_nonrenderableentity.h(21): error C2143: 语法错误:缺少 ';'在“*”和
gl_nonrenderableentity.h(21) 之前:错误 C4430:缺少类型说明符 - 假定为 int。注意:C++ 不支持每个有问题的变量成对使用 default-int
。
我知道造成这种情况的原因是在任何地方都找不到它的类型(奇怪的是,IDE 在每次分析后几乎一秒钟就发现了这一点)。有时,这些错误会在编译器的输出中重复,并且在稍微调整包含标头的方式后,每个变量仅获得一份副本(而且它仍然没有意义,我正在使用包含防护......不VS 2010 Express 理解 pragmas 吗?)
我认为这可能是一个标头问题,但对我来说最有趣的是:为什么 IDE 用红色强调变量的类型,说它找不到它,然后一两秒后它找到它,没有更多问题,但是当我编译所有项目时,它给了我上面提到的错误。
每次遇到问题时,我都会自行研究并找到解决方案,甚至是 IDE 的问题(著名的 msbuild bug)。但我就是想不通,需要各位前辈的帮助。任何人都知道为什么 IDE 会有这样的反应吗?每次分析完 IDE 后消失的红色下划线一定意味着什么。我知道当你编写新变量时会发生这种情况,直到 IDE 找到它们的声明,但这只是一次,然后无论如何都可以......对于这两个特定的类,它总是一次又一次地执行此操作(并且这些是唯一的)遇到这个问题)。其他类的其他成员变量不存在这个问题。
谢谢。很抱歉这篇文章很长,但我已经尽力让自己清楚了。如果有人想要更多信息,请......
已解决
这是一个循环引用问题,只需使用前向声明就可以了。
I almost finished writing the first draft of my game's logic engine which is entity / component based. It's not perfectly based on that philosophy, but I also think that how the implementation is done differs from person to person. I'm writing in C++ using OOP, STL and virtual functions. Yes, I know that virtual functions may be slow but it's not an exercise for doing something very fast, just a logic engine which can be easily extended and easily understood by others (everything is also commented). It's just an exercise of 'software design' by a newbie to game developing if you may wish to call it that way.
My first draft is almost complete, the core services (various inits, directX, etc) are separated from the game logic and are ready to be used. I'm just trying to finish the logic engine before I can make my first prototype game (I think about an isometric ARPG). The GL engine is built like a tree of classes, starting at the top with the basic abstract class "Entity" and deriving from it more and more specific classes. There are no more than 3 or 4 levels of inheritance anywhere for good design reasons.. I think.
But I may have a serious problem: in certain classes in which I declare as member variables objects of another class, this happens: after the IDE kicks in and analyzes what I've written, it underlines those specific member variable's type (the class name) and it says it isn't declared anywhere. Then after a split of a second, the red underline disappears and it says it's ok and declared (o.O) IT HAPPENS AGAIN AND AGAIN everytime it analyses the code. This happens also in another class with another type of class. Just to be clear, I did include all the relevant headers and it SHOULD know about the type's declaration and where is it done.
Also, if I try to compile, it gives me gl_nonrenderableentity.h(21): error C2143: syntax error : missing ';' before '*'
and gl_nonrenderableentity.h(21): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
in pairs for each offending variable.
I know that the cause of this is not finding it's type anywhere (which strangely, the IDE finds, almost a second after each analysis). Sometimes those errors would be duplicated in the compiler's output and after tweaking a little how the headers where included, I get only one copy per variable (and it still doesn't makes sense, I'm using include guards.. doesn't VS 2010 Express understand pragmas ?)
I think it may be a header problem, but the most intriguing thing for me is: why the hell the IDE underlines the variable's type with red, saying it can't find it and after a second or two it finds it, no more problem, but when I compile all the project it gives me the former mentioned errors above.
Every time I had a problem, I researched and found a fix on my own, even problems with the IDE (the famous msbuild bug). But I just can't figure this out and I need the help of the veterans. Anyone has a clue of why the IDE is reacting like that ? The disappearing red underline after each analysis of the IDE must mean something. I know this happens with new variables when you write until the IDE finds their declarations, but it's only once and then it's ok no matter what.. with those 2 specific classes, it always does again and again this (and those are the only ones having this problem). Other member variables from other classes don't have this problem.
Thank you. Sorry for the long post, but I've tried making myself clear as hard as I could. If anyone wants any more information, please..
SOLVED
It was a circular reference problem, just use a forward declaration and you're fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的唯一原因是循环引用,尽管它可能不会立即明显,但检查(并可能修复)此问题的一个简单方法是为另一个类添加前向声明:
另外,不要采取IntelliSense 检查(红色下划线)过于认真——它并不完美,并且可能会造成混淆,尤其是对于像这样的复杂引用。
更新:只要您仅使用指向对象的指针,循环引用就不会导致问题。由于指针的大小与实际对象无关,因此编译器可以毫无问题地预先分配指针。在 C# 等语言中,对象总是通过引用(指针)进行操作,因此这个问题不会出现。
The only reason I can think of is a circular reference, and even though it may not be immediately evident, a simple way to check (and possibly fix) this is to add a forward declaration for the other class:
Also, don't take the IntelliSense checking (red underlines) too seriously -- it's not perfect and can get confused, especially with complex references such as this.
Update: Circular references won't cause a problem as long as you only use pointers to objects. Because a pointer's size is independent of the actual object, the compiler can allocate a pointer beforehand without problems. In languages such as C#, objects are always manipulated via references (pointers) so this problem doesn't surface.