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
6 2429
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: - strcpy(one.name, "one" );
-
one.neighbour[0] = &two;
-
and so on
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.
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. -
class node {
-
-
...
-
-
-
setNeighbour1( node& n1,
-
node& n2,
-
node& n3 );
-
};
-
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
and a few line below you redefine (and initialize) it as follows: -
struct node one={"one",
-
{&two, &three}
-
};
-
An old C trick can be this: -
extern struct node one;
-
extern struct node two;
-
extern struct node three;
-
This turns the three in just declarations.
kind regards,
Jos
Thanks for the help. I started rearchitecting my code, but decided that simply using extern would fix the issue.
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: -
node one; //calls default constructor
-
This is C++ and not C and this is one place where the two languages part company.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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:...
|
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...
|
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...
|
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 :
...
|
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...
|
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)...
|
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>
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
| |