I have a line of code that I am getting an error at the first -> (which I am sure the rest of them are wrong as well) can someone tell me what I am missing?
Character *m_pHead -> Character *m_pNext -> Character *m_pNext -> Character *m_pNext = NULL;
The idea is to create a list of characters where Character is a class.
Yes, I want to use it to reference a pointer from an added header file ("CharacterList.h"). The list will be passed the values by the instructors test cpp file. He gave us the code stated above in class and said we can use something like that. Here is the code I have thus far for the program (I am getting other errors but I want to focus on 1 at a time instead of trying to tackle them all at the same time. I will comment the errors that are popping up.):
When he wrote that he probably didn't mean C++'s -> operator. He probably just used an arrow to describe that you have a head pointer that points to a character that has another pointer that points to the next character in the list, and so on...
The m_pHead pointer should probably be located in the CharacterList, and m_pNext should probably be in the Character class.
MikeyBoy - that is the deconstructor that he gave us in his statement of work. Everything you see there are things he requires in the program for his test program to work properly for our grade. All function names were given.
Peter87 - understood. I will give that a go and see what happens.
That can't possibly be a piece of C++ code he gave you. I'm inclined to agree with Peter87 - you've radically misunderstood some kind of explanatory diagram that your instructor drew for you.
Sorry, I'm thinking what I am seeing as line 26 on my phone is not what line 26 actually is. If you are speaking about where the first error is (shows as line 42 on my phone), yes I believe you are both correct. I had to of misunderstood him. I'm going to play with it a little to see what I come up with and post my success or failure.
BTW, line 26 shows CharacterList::~CharacterList() on my phone.
Lol your first response I thought it was fairly clear what line 26 was. Your second response made me look again at the post because I thought I was going crazy. I'm working on the code now and will post again soon.
When you have errors, it helps if you tell us what the errors are. Otherwise, you're making our job harder.
At line 34, newCharacter -> *characterName = characterName makes no sense. *characterName isn't a member of your class, characterName is.
Also this line is after the end of your if statement, which means that the following else block isn't connected to anything, and is therefore illegal. Your else block is empty anyway, so what's the point of it?
At line 51, you've defined a variable called temp, of type Character. Immediately after, at line 52, you're trying to declare another variable of the same name, in the same scope. That's illegal, for obvious reasons.
Also, you haven't seen fit to show us the definition of your Character class, but I'm guessing m_pHead is a pointer. So it won't be the same type as this version of temp.
At line 56, you're declaring another variable of the same name, which is legal, because you're now in a different scope, although it's a bad idea, because it's hiding the definition of temp in the outer scope. But you've not defined it to be a pointer, so using the -> operator makes no sense.
Line 80 has several problems. It looks as though you're trying to define a variable called newItem->m_pNext. That makes no sense - if you want to define a member of a class, you do it in the class definition, not like this.
Also, again, it looks like you're trying to define the variable to be a Character, and (again, I have to guess) m_pNext is probably a pointer.
#ifndef ITEM_H
#define ITEM_H
class Character
{
private:
char m_sName[64];
int m_iClass, m_iAlignment, m_iHitPoints;
int m_iCharTraits[6];
Character *m_pNext;
public:
Character();
~Character();
Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr);
int getVariable();
void setVaraible();
int getName(char *name);
void setName(char *name);
int getClass(int& cl);
void setClass(int cl);
int getAlignment(int& al);
void setAlignment(int al);
int getHitPoints(int& hp);
void setHitPoints(int hp);
int getStrength(int *str);
void setStrength(int str);
int getDexterity(int *dex);
void setDexterity(int dex);
int getConstitution(int *cn);
void setConstitution(int cn);
int getIntelligence(int *itl);
void setIntelligence(int itl);
int getWisdom(int *wis);
void setWisdom(int wis);
int getCharisma(int *chr);
void setCharisma(int chr);
bool addItem(Item *item);
Item getItem(char *itemName);
Item *dropItem(char *itemName);
void printAll();
};
#endif
Is this not creating a newItem with an Item name with the dValue, dWeight, and iType values assigned to the same characterName and then setting m_pNext to NULL to signify the end of the newItem?
Is this not creating a newItem with an Item name with the dValue, dWeight, and iType values assigned to the same characterName and then setting m_pNext to NULL to signify the end of the newItem?
There's nothing in that code snippet that creates a new object, but you've done that a few lines before that code snippet, at line 72.
The error on line 80 - or line 5 of the snippet - is because you've started the line with Character. What's that doing there? You haven't done it for any of the preceding lines, which are also assigning values to data members, so why did you decide to put it here?
Starting that statement with a type makes that line make no sense.
EDIT:
Also, it looks like you're trying to set all those data members to the same value, *characterName. That makes no sense. Why would the weight (for example) of an item be the name of a character? You say:
with the dValue, dWeight, and iType values assigned to the same characterName
but what does that even mean? A character name doesn't have dValue, dWeight, and iType values. A Character doesn't have dValue, dWeight, and iType values. Those are properties of an Item, not a Character.