C++ class definition with a member of the same class

Hi, i have a question about C++. Is it possible to declare a class with a member ot the same class? For example, a linked list or i want to convert this C code to C++ class (Elemento)

typedef struct elemento
   {
      char name[40];
      char value[100];
      List<struct elemento> ltElementos;
   } s_elemento;

typedef List<s_elemento> ListElementos;

Convert to:

   public class Elemento
   {
       string name;
       string value;
       List<Elemento> ltElemento;
       
       ......
   
   }
   

If i compile that, the error is:

field `Elemento' has incomplete type

May someone help me?

Several things about this post make me think you're asking something wrong:

  • Your "C" code includes a template. That's not C. Are you using some extension or something?
  • Your "C++" code includes "public class ..." - the public keyword is misplaced there, and should generate its own warning before the one you point out.
  • Your C++ code includes a List, not a list.

Is this really C and C++ code, respectively? Also, is the error you show the very first error the compiler generates?

You should be able to declare what you did. For example, this:

#include <list>

class Thing {
    std::list<Thing> stuff;
};

Compiles just fine for me, even with the -Wall, -Wextra and -pedantic flags.

Could you give a short (but full and compilable) example that demonstrates your problem?

Hi, and thanks for your help.

Oh, yes, of course. I was wrong and have mixed Java code with my C++ code. I was trying in Java to compare and my post was wrong.

About your questions:

  1. I am programming with C++ with "typedef structs" and i want to try it with C++ class.
  2. Yes, "public" is wrong.

Then, i 'll try what you comment.

So, thanks, and sorry by my mistakes.

I don't think you can. Think about it: Wouldn't that class inside the class, also have a class inside the class? Which would also have a class inside the class. etc. etc. etc. If you want different things to have different members, they have to be different structures.

This doesn't even work in C. Observe:

typedef struct whatever
{
    int a;
    struct whatever s;
} whatever;

will give an error since you can't nest that way.

You can cheat by using a pointer. The compiler will let you make pointers to incomplete types since a pointer's always the same size.

typedef struct linkedlist
{
    int payload;
    struct linkedlist *next;
} linkedlist;

That way it only allocates the size of the pointer, not the entire struct, avoiding the infinite recursion. This means you have to explicitly allocate memory to it later to use it, of course.

Always keep in mind that you are bound to get an error generated by the compiler (irrespective of the compiler's origin) that if its not following the below rule (although unstated anywhere in books -its my own logic to predict a code's compilability ) -

"unless you are able to calculate the size of a class / struct; manually (by simple addition of all the member's size); its not gona compile."

Now apply this rule and try calculating the size off the class, which you have define. Are you not going into an infinite loop of calculation?

The above definition are not gona work. :slight_smile:

You may wish to call it praveen's compilability rule :wink:

Any comments?

---------- Post updated at 11:17 AM ---------- Previous update was at 11:03 AM ----------

Absolutely true!!!
However, here again "The Rule" works well to predict what's compilable and what's not.

In the later example, the pointer :

struct linkedlist *next;

scores 4-bytes /8-bytes respectively on 32-bit / 64-bit systems and hence enabling you to calculate the size of the struct.

However, its simple examples however the real advantages come when you have got complicated scenarios and / or working in environments where you can not afford to compile more frequently to see the results (like an outdated embedded programming environment or with cross compilers on 486 machine hosts :slight_smile:

Corrona688 and Praveen_218: What's really being asked about here is using a class as a template parameter within itself (despite what the title may suggest). You can do that - not sure what restrictions there are on that, but it's definitely possible.

I'm sure if the OP has a specific problem, they'll post a short snippet of code that demonstrates it.

get rid of typedef.

struct elemento {
    list<elemento> v;
};

not sure why you'd want a list of elementos inside an elemento though.
I wouldn't do it.

also, if using C++ prefer the std::string over char *

the string an STL classes are very fast indeed and make life much easier.

Thanks everyone.

I was wrong and my new tests confirm that is possible:

#include "string"
using namespace std;

#include <list>

class Elemento
{
   public:
      string name;
      list<Elemento> children;

..........
};

Sorry, my first post wasn't very good. I didn�t explain it well.:frowning:

Thanks