开发者

ClientClass does not name a type. GCC Linux

While making my code, i ran into a strange problem. I hold 1 file for all includes, lets call it includes.h and class files like clientclass.h etc.

The problem is, when i try to compile my code i get a compiler error:

/mnt/orange-new/units/includes.h|34|error: ‘ClientClass’ does not name a type|

includes.h :

#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

Can you help me with my problem? im out of ideas :(


You have a circular dependency: includes.h -> clientclass.h -> includes.h. How this is resolved depends on which header gets included first, but it's always going to be confusing. Most likely it's causing the line

#include <clientclass.h>

to succeed but fail to include the content, since the include guard CLIENTCLASS_H_INCLUDED has already been defined even though the content doesn't exist yet.

To resolve this, you may just be able to remove the include of includes.h from clientclass.h, if it's not used for anything. If you use the types from includes.h you can use forward-declarations, which declare that a class exists without defining it, e.g.

class ClientClass;

That way you can use pointers and references to ClientClass without having to include clientclass.h. What you can't do is declare values of forward-declared types, since the compiler has to know everything about the type (at least, how large it is) before it can reserve memory for a value of that type. If you need this, you may have to break your header down into smaller parts and include just the small part that you depend on.

So, for example, you can do the following:

class MyClass;

MyClass * globalPointer;

void doSomething(const MyClass & foobar);

without having a definition of MyClass in scope. The two expressions here only use MyClass via a pointer or reference. But the following won't work:

class MyClass;

void doSomethingElse() {
    MyClass theobject;
    doSomething(theobject);
}

This requires space to be reserved on the stack for an object of type MyClass. Without a definition of MyClass in scope, the compiler has no way of knowing how much memory to allocate.

In your case, you are defining global values of type ClientClass, and this requires a full definition of ClientClass, not just a forward declaration. You have a couple of options:

  • Break down the include files further so that you can include just the small part you need
  • Hold your global value by pointer, and allocate it somewhere later in the code, after you have included the full definition of ClientClass

Another option is to reconsider whether global variables are the right solution here at all.


I am kind of confused why you have a clientclass.h which includes includes.h which includes clientclass.h

I think the issue might be in there somewhere. You shouldn't be doing that.


Maybe it is because of cross including.. did you try a forward declaration of the type by adding

class ClientClass;

inside includes.h before ClientClass Clients


after trying to use forward declaration i end up with a error:

/mnt/orange-new/units/includes.h|37|error: aggregate ‘ClientClass Clients’ has incomplete type and cannot be defined|

edit: splitting includes.h into 2 files classes.h and includes.h resolved the problem. Im very thankfull everyone for helping. I never expected so much answers in so low time :) expect me to be here more often. Thank You.


As your headers are laid out, it would be impossible to include "clientclass.h" anywhere else but from "includes.h". That's because in this case the global instances would end up being declared before the definition of the classes in "clientclass.h"

If you include "clientclass.h", then the first thing it does is to pull in the "includes.h" including the globals declared with it.

Try including only the things that the "clientclass.h" header needs: <string> (and the header where MAX_CLIENTS comes from).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜