473,320 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

circular struct reference in c++

3
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
Jul 19 '07 #1
6 2429
ravenspoint
111 100+
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:

Expand|Select|Wrap|Line Numbers
  1. strcpy(one.name, "one" );
  2. one.neighbour[0] = &two;
  3.  
and so on
Jul 19 '07 #2
lloopy
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=&Bob;

------------------------------
than this:
=====================
struct node node1={"node1",
{&node2,&node3,&node4},
{&node2,&node5},
{&node3,&node6},
1,3,6,&Bob
};
======================
...which worked just fine in C.
Jul 19 '07 #3
ravenspoint
111 100+
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.
Expand|Select|Wrap|Line Numbers
  1.      class node {
  2.  
  3.          ...
  4.  
  5.  
  6.              setNeighbour1( node&  n1,
  7.                 node& n2,
  8.                 node& n3 );
  9.      };
  10.  
Jul 19 '07 #4
JosAH
11,448 Expert 8TB
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

Expand|Select|Wrap|Line Numbers
  1. struct node one;
  2.  
and a few line below you redefine (and initialize) it as follows:

Expand|Select|Wrap|Line Numbers
  1. struct node one={"one",
  2.      {&two, &three}
  3. };
  4.  
An old C trick can be this:

Expand|Select|Wrap|Line Numbers
  1. extern struct node one;
  2. extern struct node two;
  3. extern struct node three;
  4.  
This turns the three in just declarations.

kind regards,

Jos
Jul 19 '07 #5
lloopy
3
Thanks for the help. I started rearchitecting my code, but decided that simply using extern would fix the issue.
Jul 20 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
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:

Expand|Select|Wrap|Line Numbers
  1. node one;    //calls default constructor
  2.  
This is C++ and not C and this is one place where the two languages part company.
Jul 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vera | last post by:
I have two assemblies that each consist of several classes. Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those...
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
16
by: Kiuhnm | last post by:
Is there an elegant way to deal with semi-circular definitions? Semi-circular definition: A { B }; B { *A }; Circular reference: A { *B }; B { *A }; The problems arise when there are more...
3
by: JCB | last post by:
Hi, I have two C file which are referencing in a circular way. To simplify I use an example with car : Suppose I have a program for cars. I have car.c, structure car and functions...
2
by: Mark Mackey | last post by:
Hi all. I've got a rather complicated circular type definition, and would appreciate help getting it sorted out :). Cut-down code below : ...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
6
by: Stephen Robertson | last post by:
We are currently in a dead end with a circular reference issue using vb.net, and are hoping someone might help us resolve it. Idea... We have frmmain calling frmperson (dim f as new frmperson)...
3
by: Boris | last post by:
I was making a part of an application more and more generic when I suddenly ended up with code like this: template <class T> struct MenuItems { T &m; MenuItems(T &m); }; template <class T>
3
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi I Have a solution with about 50 projects and each project have References to 1 to n of the projects in the solution. I try go to a project and try to add a reference to another project and I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.