circular struct reference in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lloopy
    New Member
    • Jul 2007
    • 3

    circular struct reference in c++

    I am trying to represent a map using nodes. This code segment seems to work just fine in C, but craps out in C++.

    struct node{
    char name[50];
    struct node* neighbors[6];
    };

    struct node one;
    struct node two;
    struct node three;

    struct node one={"one",
    {&two, &three}
    };

    struct node two={"two",
    {&one, &three}
    };

    struct node three={"three",
    {&two, &one}
    };
    --------------------------
    My problem is that when I try to do this in C++, I get complaints of redefinitions. If I don't do the forward declaration, then I get unknown object errors.

    I'm fundamentally missing something, I'm sure.

    -lloopy
  • ravenspoint
    New Member
    • Jul 2007
    • 111

    #2
    Originally posted by lloopy
    I am trying to represent a map using nodes. This code segment seems to work just fine in C, but craps out in C++.

    struct node{
    char name[50];
    struct node* neighbors[6];
    };

    struct node one;
    struct node two;
    struct node three;

    struct node one={"one",
    {&two, &three}
    };

    struct node two={"two",
    {&one, &three}
    };

    struct node three={"three",
    {&two, &one}
    };
    --------------------------
    My problem is that when I try to do this in C++, I get complaints of redefinitions. If I don't do the forward declaration, then I get unknown object errors.

    I'm fundamentally missing something, I'm sure.

    -lloopy

    Why not use something like this:

    Code:
    strcpy(one.name, "one" );
    one.neighbour[0] = &two;
    and so on

    Comment

    • lloopy
      New Member
      • Jul 2007
      • 3

      #3
      The code sample I included was a demonstration of the concept.

      The actual structure is somewhat more complicated.

      There are 120 nodes. Each node keeps a list of three types of neighbors, as well as 7 other pieces of information.

      This seems more cumbersome:
      ------------------
      node1.neighbor_ type1[0]=&node2;
      node1.neighbor_ type1[1]=&node3;
      node1.neighbor_ type1[2]=&node4;

      node1.neighbor_ type2[0]=&node2;
      node1.neighbor_ type2[1]=&node5;

      node1.neighbor_ type3[0]=&node3;
      node1.neighbor_ type3[1]=&node6;

      node1.size=1;
      node1.type=3;
      node1.strength= 6;
      node1.owner=&Bo b;

      ------------------------------
      than this:
      =============== ======
      struct node node1={"node1",
      {&node2,&node3, &node4},
      {&node2,&node5} ,
      {&node3,&node6} ,
      1,3,6,&Bob
      };
      =============== =======
      ...which worked just fine in C.

      Comment

      • ravenspoint
        New Member
        • Jul 2007
        • 111

        #4
        You should consider making your node a class, with a constructor and assignment methods. Properly designed, this would allow you to initialize everything with a minimum of cumber.
        [CODE=cpp]
        class node {

        ...


        setNeighbour1( node& n1,
        node& n2,
        node& n3 );
        };
        [/CODE]
        Last edited by ravenspoint; Jul 19 '07, 06:40 PM. Reason: add code

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by lloopy
          I am trying to represent a map using nodes. This code segment seems to work just fine in C, but craps out in C++.

          struct node{
          char name[50];
          struct node* neighbors[6];
          };

          struct node one;
          struct node two;
          struct node three;

          struct node one={"one",
          {&two, &three}
          };

          struct node two={"two",
          {&one, &three}
          };

          struct node three={"three",
          {&two, &one}
          };
          --------------------------
          My problem is that when I try to do this in C++, I get complaints of redefinitions. If I don't do the forward declaration, then I get unknown object errors.

          I'm fundamentally missing something, I'm sure.

          -lloopy
          C++ considers them redefinitions because first you (tentatively) defined, say,
          your struct one as

          [code=cpp]
          struct node one;
          [/code]

          and a few line below you redefine (and initialize) it as follows:

          [code=cpp]
          struct node one={"one",
          {&two, &three}
          };
          [/code]

          An old C trick can be this:

          [code=cpp]
          extern struct node one;
          extern struct node two;
          extern struct node three;
          [/code]

          This turns the three in just declarations.

          kind regards,

          Jos

          Comment

          • lloopy
            New Member
            • Jul 2007
            • 3

            #6
            Thanks for the help. I started rearchitecting my code, but decided that simply using extern would fix the issue.

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Originally posted by JosAH
              C++ considers them redefinitions because first you (tentatively) defined, say,
              your struct one as


              Code: ( cpp )
              struct node one;
              or just omit the struct keyword:

              [code=cpp]
              node one; //calls default constructor
              [/code]

              This is C++ and not C and this is one place where the two languages part company.

              Comment

              Working...