Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :(

like image 566
Knobik Avatar asked Feb 13 '26 11:02

Knobik


2 Answers

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.

like image 119
Tim Martin Avatar answered Feb 15 '26 00:02

Tim Martin


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.

like image 43
user606723 Avatar answered Feb 15 '26 02:02

user606723



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!