Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 5th, 2006, 8:39 AM   #1
Kry1010
Newbie
 
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0 Kry1010 is on a distinguished road
Linking to a linked list within a linked list

Hello,

I'm having problems with linking to a linked list within a linked list. I'm using two different structs within my class 'Players';

	class Players
{
 private:
        struct playerRec
	{
		char name[25];
		int age;
		int goals;
		struct historyRec* historyPtr;
		struct playerRec* next;
	};

	struct playerRec startPtr;

	struct historyRec
	{
		char club[25];
		int dateJoined;
		int dateLeft;
		struct historyRec* next;
	};
};

Setting up players is working fine, i.e. making a new playerRec node and assigning each of the fields/elements and linking those to other playerRec nodes is fine, the problem i'm having however is creating a new node for the 'historyPtr' to point to, this may help you to understand what I'm saying:



I can create a new 'historyRec' node using;

struct historyRec* tmpPtr = new struct historyRec;

but when I try to tell 'historyPtr' (in my playerRec) to point to this new node using the following code:

startPtr->historyPtr = tmpPtr

I get the following error - "Cannot convert from struct Players::historyRec * to struct historyRec*"

Likewise when I try the following:

 startPtr->historyPtr = new struct historyRec;

Sorry if this is something really simple I have overlooked, I'm fairly new programming especially linked lists.

Can anyone see what I'm missing/doing wrong, any help would be greatly
appreciated, many thanks.
Kry1010 is offline   Reply With Quote
Old Apr 5th, 2006, 9:03 AM   #2
Xyhm
Programmer
 
Xyhm's Avatar
 
Join Date: Mar 2006
Posts: 60
Rep Power: 3 Xyhm is on a distinguished road
First you declare startPtr as "struct playerRec startPtr;" (i.e, a non-pointer), then you try to use it as a pointer with "startPtr->historyPtr = tmpPtr".

The declaration should either be "struct playerRec * startPtr" (and then of course then you need to fill that pointer with the appropriate struct), or the assignments to startPtr should be in the form "startPtr.historyPtr = tmpPtr". That is, with "." and not "->".

Or at least that's what I think after a fairly quick look.

Last edited by Xyhm; Apr 5th, 2006 at 9:16 AM.
Xyhm is offline   Reply With Quote
Old Apr 5th, 2006, 9:16 AM   #3
Kry1010
Newbie
 
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0 Kry1010 is on a distinguished road
Sorry, I missed out the asterisk when I manually entered some the code I wasn't originally going to show, but can you see any problems with it still?
Kry1010 is offline   Reply With Quote
Old Apr 5th, 2006, 10:14 AM   #4
Animatronic
Programmer
 
Join Date: Jun 2005
Posts: 99
Rep Power: 4 Animatronic is on a distinguished road
	class Players
{
 private:
        struct playerRec
	{
		char name[25];
		int age;
		int goals;
		struct historyRec* historyPtr;
		struct playerRec* next;
	};

	struct playerRec startPtr;

	struct historyRec
	{
		char club[25];
		int dateJoined;
		int dateLeft;
		struct historyRec* next;
	};
};

These are forward declarations for a struct named historyRec, your historyRec has the full name Players::historyRec.

In C to reffer to a struct you would do struct historyRec, in C++ the struct part isnt needed, as historyRec is a new type, and is only used like this todo forward declarations.

To fix your problem change struct historyRec to just historyRec (this may not work havent checked) or Players::historyRec.

Also you will probably have to move the definition for historyRec above playerRec (or use a forward declaration ).
Animatronic is offline   Reply With Quote
Old Apr 5th, 2006, 10:46 AM   #5
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 825
Rep Power: 4 The Dark is on a distinguished road
I'm pretty sure to get that to complile, you must have another historyRec struct declaration around somewhere. Possibly you have a forward declaration that is not in the class. Put the forward declaration in the class.
The Dark is offline   Reply With Quote
Old Apr 6th, 2006, 5:16 AM   #6
Kry1010
Newbie
 
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0 Kry1010 is on a distinguished road
Thanks a lot Animatronic, I tried what you said and it works now... I'm so happy :banana:
Kry1010 is offline   Reply With Quote
Old Apr 6th, 2006, 6:21 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Most use a typedef for structs so one doesn't have to do it in C either.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC