在编写代码时,我遇到了一个奇怪的问题。 我为所有包含1个文件,我们称之为includes.h和像clientclass.h等类文件。
/mnt/orange-new/units/includes.h|34|error:'ClientClass'没有命名一个types|
includes.h:
POSIX线程如何在Linux中工作?
简单的JPG库(DLL)只用于保存JPG图像?
获取/ dev / video0的硬件信息
如何在Linux上通过inode访问文件
Ubuntu 11.10上的C和C ++编程
#ifndef INCLUDES_H_INCLUDED #define INCLUDES_H_INCLUDED #include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <errno.h> #include <sys/timeb.h> #include <sys/select.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/wait.h> #include <netinet/in.h> #include <fcntl.h> #include <arpa/inet.h> #include <time.h> #include <iostream> #include <cstring> #include <string> #include "config.h" #include "console.h" #include "clientclass.h" #include "tcpparser.h" #include "netmsg.h" #include "main.h" Console Konsola; ClientClass Clients; TCPThread ParserTCP; #endif // INCLUDES_H_INCLUDED
clientclass.h:
#ifndef CLIENTCLASS_H_INCLUDED #define CLIENTCLASS_H_INCLUDED #include "includes.h" struct ClientStruct { int Sock; int Ident; int Room; std::string Name; std::string IP; }; class ClientClass { public: ClientClass(); // create int Add(); void Delete(int index); int Count(); ClientStruct Client[MAX_CLIENTS]; protected: void Reset(int index); private: int _count; }; #endif // CLIENTCLASS_H_INCLUDED
你能帮我解决我的问题吗? 即时通讯的想法:(
我在哪里可以findsql Server版本的友好的string?
使用C#获取连接到命名pipe道服务器的客户端的进程ID
在VC ++编译的应用程序中使用G ++编译的DLL(插件)时会出现什么问题?
使用用于.NET控件的WiX安装程序在32个版本上设置registry项。 64位,WoW6432Node
如何测量C#中的系统空闲时间,不包括看电影等?
你有一个循环依赖: includes.h -> clientclass.h -> includes.h 。 如何解决这个问题取决于首先包含哪个头,但总是会令人困惑。 最有可能的是这个线路
#include <clientclass.h>
成功但未能包含内容,因为即使内容还不存在,包含警卫CLIENTCLASS_H_INCLUDED也已经被定义。
为了解决这个clientclass.h ,如果不使用任何东西,你可能只能从clientclass.h删除包含includes.h 。 如果你使用includes.h的类型,你可以使用forward-declarations,它声明一个类没有定义它,例如
class ClientClass;
这样你就可以使用ClientClass指针和引用而不必包含clientclass.h 。 你不能做的是声明前向声明类型的值 ,因为编译器必须知道类型的所有内容(至少它有多大),才能为该类型的值保留内存。 如果你需要这个,你可能不得不把你的头部分成更小的部分,只包括你依赖的小部分。
因此,例如,您可以执行以下操作:
class MyClass; MyClass * globalPointer; void doSomething(const MyClass & foobar);
没有在范围内定义MyClass 。 这里的两个表达式只通过指针或引用使用MyClass 。 但以下将无法正常工作:
class MyClass; void doSomethingElse() { MyClass theobject; doSomething(theobject); }
这需要在堆栈上为MyClass类型的对象保留空间。 如果没有在范围内定义MyClass ,编译器无法知道要分配多少内存。
在你的情况下,你正在定义ClientClass类型的全局值,这需要ClientClass的完整定义,而不仅仅是一个前向声明。 你有几个选择:
进一步分解包含文件,以便只包含所需的小部分
在包含ClientClass的完整定义之后,通过指针保存全局值,并在代码稍后的某个地方分配它
我有点困惑,为什么你有一个clientclass.h,其中包括includes.h包括clientclass.h
我认为这个问题可能在某个地方。 你不应该这样做。
class ClientClass;
在ClientClass Clients之前的ClientClass Clients
/mnt/orange-new/units/includes.h|37|error:aggregate'ClientClass Clients'具有不完整的类型,无法定义|
编辑:splitting includes.h到2个文件classes.h和includes.h中解决了这个问题。 我非常感谢所有人的帮助。 在这么短的时间里,我从来没有想到会有这么多答案:)希望我能经常到这里来。 谢谢。
当你的头文件被打好的时候,就不可能在“includes.h”的其他地方加入“clientclass.h”。 这是因为在这种情况下,全局实例最终将在“clientclass.h”中的类的定义之前被声明。
如果你包含“clientclass.h”,那么它所做的第一件事就是引入“includes.h”,包括用它声明的全局变量。
尝试仅包含“clientclass.h”头文件所需的内容 : <string> (以及MAX_CLIENTS所在的头文件)。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。