C++ 类读取为变量,默认类型 int? 说什么?
所以,我有两个类......结构非常基本。 我尝试将一个导入到另一个中,并声明该类类型的一个新对象...但是,它似乎将类名读取为变量的名称?!
下面提供的标头类将无法正确读取“ApplicationManager”类。 代码:
####ifndef _GAME_H_
####define _GAME_H_
####include "application.h"
####include "applicationmanager.h"
class Game : public Application
{
public:
Game();
~Game();
void LoadContent() override;
void UnloadContent() override;
void Draw() override;
private:
//int ApplicationManager; //WHY DOES THIS COMPILE??!
ApplicationManager management; //This DOES NOT WORK?
};
####endif
这是“ApplicationManager”类的标头。 代码:
####ifndef _APPMANAGER_H_
####define _APPMANAGER_H_
####include "game.h"
####include "application.h"
class ApplicationManager
{
public:
ApplicationManager(void);
~ApplicationManager(void);
private:
};
####endif
发生的错误告诉我我需要一个“;” 在“management”之前,并且“ApplicationManager”缺少类型说明符,因此假定它是默认类型 int。
...有什么想法为什么它不能正确编译吗? 其他人可以尝试这个并报告结果吗? 我复制了代码,并将其粘贴到不同的解决方案中,看看是否有某些内容已损坏......它仍然无法工作。
So, I have two classes...Very basic in structure. I try to import one into the other, and declare a new object of that class type...however, it seems to read the class name as the name of a variable?!
The header class provided below will not read the "ApplicationManager" class properly.
Code:
####ifndef _GAME_H_
####define _GAME_H_
####include "application.h"
####include "applicationmanager.h"
class Game : public Application
{
public:
Game();
~Game();
void LoadContent() override;
void UnloadContent() override;
void Draw() override;
private:
//int ApplicationManager; //WHY DOES THIS COMPILE??!
ApplicationManager management; //This DOES NOT WORK?
};
####endif
Here is the header for the "ApplicationManager" class.
Code:
####ifndef _APPMANAGER_H_
####define _APPMANAGER_H_
####include "game.h"
####include "application.h"
class ApplicationManager
{
public:
ApplicationManager(void);
~ApplicationManager(void);
private:
};
####endif
The error that occurs, tells me that I need a ";" before "management", and that "ApplicationManager" is missing a type specifier, so it is assumed to be default-type int.
...any ideas why it won't compile properly? Can someone else try this and report the results? I copied the code, and pasted it in a different solution, to see if something became corrupted....it still didn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你有一个循环引用。 当从
applicationmanager.h
包含game.h
时,编译器尚未读取ApplicationManager
类。删除该行。
要修复此问题,请从
applicationmanager.h
中You have a cyclic reference. When
game.h
is included fromapplicationmanager.h
, theApplicationManager
class has not yet been read by the compiler.To fix, remove the line
from
applicationmanager.h
.为什么
Game.h
和AppicationManager.h
之间存在循环依赖关系?除此之外,我建议检查
Application.h
中的标头防护 (#ifdef _*_H
)。 在 C++ 中,当复制/粘贴代码或复制文件时,经常发生的情况是忘记更改新类的标头保护定义名称,因此最终会得到由同一定义保护的两个不同标头。 在这种情况下,如果两者都包含在其他文件中,则只有第一个文件实际上会扩展为任何有用的内容。Why do you have circular dependency between
Game.h
andAppicationManager.h
?Aside from that, I'd say check your header guard (
#ifdef _*_H
) inApplication.h
. A fairly often occurence in C++, when copy/pasting code or copying files, is to forget to change the header guard define name for a new class, so you end up with two different headers guarded by the same define. In which case, if both are included into some other file, only the first will actually be expanded into anything useful.错误消息有些误导性。 它基本上是说“由于某种原因(可能是引用类型中的错误)我无法识别您正在使用的类型(在您的情况下是 ApplicationManager)”。
如果您需要 ApplicationManager 了解 Game,请创建一个纯虚拟基类(其他术语中的接口),并让 Game 继承该基类(不扩展接口),并让 ApplicationManager 包含基类头文件
THe error message is some what misleading. It basically says "For some reason (probably an error in the referenced type) I cannot recognize the type you're using (in you case ApplicationManager)".
If you need ApplicationManager to know about Game make a pure virtual base class (interface in other terms) and have Game inherit from that (with out extending the interface) and have ApplicationManager include the base class header file