#recursive structs

1 messages · Page 1 of 1 (latest)

harsh loom
#

hey, so I wanted to create something similar to this


struct a{
  struct b* ptr;
  int a, b, c;
}

struct b
{
  struct a a_val;
}

but it gives me an error type "b" does not name a type could anyone help?

tacit gyroBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

slender mason
#

you need to forward declare it

harsh loom
slender mason
#

no, thats not a forward declaration

livid forum
#

;compile

struct a{
  struct b* ptr;
  int a, b, c;
};

struct b
{
  struct a a_val;
};
sullen zincBOT
#
Compilation successful
livid forum
#

This already compiles

#

If I add the ;s

harsh loom
#

wait a minute

slender mason
#
struct Foo; // declaration
// use Foo* or Foo&
struct Foo { // provide definition somewhere

};
``` is the typical route
livid forum
#

This already compiles
But this is written in C style. In C++ you'd typically write it as

struct a{
  b* ptr;
  int a, b, c;
};

struct b
{
  a a_val;
};

And this indeed doesn't compile until you add struct b; at the beginning before the first struct

livid forum
#

The C style works in C++ as well

livid forum
#

The C style works in C++ as well
It just looks ugly

harsh loom
#

I haven't done that in my original code

#

I will sent it to you right now

harsh loom
# livid forum > The C style works in C++ as well It just looks ugly

struct ElifConditionals
{
  Value* exp;
  Program block;
  // ElifConditionals(**& v_exp, Program*& v_block)Value*:exp(v_exp), block(v_block){
  //   Node = NodeType::ElifConditionals_;
  // }
};

struct ElseConditionals
{
  Program block;
  // ElseConditionals(Program*& v_block):block(v_block){Node = NodeType::ElseConditionals_; }
  
};



struct Value
{ // types of expression
  // statements
  NodeType Node;
  union
  {
    E_O_F eof;
    Program program;
    //VarDec varDec;
    //VarConstDec varConstDec;
    //Conditionals conditionals;
    //ElifConditionals 
    //ElseConditionals,

    // both literals(value) and expressions
    BoolLiteral boolLiteral;
    IntLiteral intLiteral;
    NullVal nullval;

    // expressions
    AssignExpr assignExpr;
    Identifier identifier;
    BinaryArithExpr binaryArithExpr;
    BinaryBoolExpr binaryBoolExpr;
    BinaryBoolNumExpr binaryBoolNumExpr;
  };
  
};

it gives me error

#
src/Headers/../FrontEnd/Headers/AST.h:124:3: error: ‘Value’ does not name a type
  124 |   Value* exp;
      |   ^~~~~```
harsh loom
#

it is compiled in c++

livid forum
#

Read the last sentence of the message I linked

harsh loom
#

oh

#

thanks

#

!solved