![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Mar 2006
Posts: 60
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0
![]() |
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?
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Jun 2005
Posts: 99
Rep Power: 4
![]() |
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 ). |
|
|
|
|
|
#5 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
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.
|
|
|
|
|
|
#6 |
|
Newbie
Join Date: Apr 2006
Location: UK
Posts: 3
Rep Power: 0
![]() |
Thanks a lot Animatronic, I tried what you said and it works now... I'm so happy :banana:
|
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|