LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2005, 10:07 AM   #1
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Rep: Reputation: 16
Circularly defined structures in C++


This may have a very simple answer, but if so it just keeps eluding me. I need to do something like this:
Code:
struct foo {
     int id;
     bar *bp;
};

struct bar {
     int id;
     foo *fp;
};
Using something like that just errors as bar is undeclared whilst foo is being declared. Is there a way to get around this? I am currently just declaring the pointers as void, then casting them when I need to use them. Obviously this is not ideal.

Any ideas would be greatly appreciated
 
Old 10-30-2005, 10:41 AM   #2
naf
Member
 
Registered: Oct 2005
Location: Chicago, USA
Distribution: Slackware & Fedora
Posts: 66

Rep: Reputation: 15
Use forward declaring:
Code:
struct foo {
     int id;
     struct bar *bp;
};

struct bar {
     int id;
     struct foo *fp;
};
 
Old 10-30-2005, 12:03 PM   #3
infinity42
Member
 
Registered: Apr 2005
Location: England
Distribution: Gentoo
Posts: 142

Original Poster
Rep: Reputation: 16
Ah, thank you so much. I can't believe it was that simple!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding structures eshwar_ind Programming 2 04-25-2005 09:18 AM
initializing structures within functions.. miguetoo Programming 5 03-02-2004 07:50 PM
Structures AMMullan Programming 6 02-18-2004 11:39 AM
Nested structures :S? alitrix Programming 11 11-15-2003 07:13 PM
C and arrayed structures.. miguetoo Programming 9 05-22-2003 06:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration