Hi everyone,
How do you define two structs that include lists of or pointers to each other? I've tried including a forward declaration of one, and neither way works. Any help getting around this would be great, thanks in advance!

// struct arc; 

struct node {
	Vector<arc> arcs;
	void toString() {
		for(int i=0; i<arcs.size(); i++) {
			arcs[i].toString();
		}
	}
};

struct arc {
	node *one;
	node *two;
	void toString() {
		cout << one->name << two->name;
	}
};

Recommended Answers

All 6 Replies

No, the answer is not that you're designing anything wrong. There is nothing wrong at all about mutually recursive data datatype definitions. There are inconveniences involved with circular data structures. And there were problems in your case.

thingstealer, a forward declaration doesn't work? Could you paste the exact code at http://codepad.org/, set it to C++ mode, and give the link? I'd like to see the error message.

Oh, it's just looking for a main function to run.

http://codepad.org/teq2fuVp

It seems to work fine. You have an implementation of Vector, right?

This codepad is nice!!

Yeah, I do. Strange. When I run it (on XCode) it says error: forward declaration of arc and error: invalid use of undefined type 'struct arc'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.