Not sure how to use -> operator

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.
Only the last part of that statement makes any sense.
 
Character *m_pNext = NULL;  // Initialize pointer to NULL 


-> is the reference operator. It allows you to reference a member variable using a pointer. i.e.
1
2
 
  m_pNext->member_var = 0;


If you want to create a list, use std::list
http://www.cplusplus.com/reference/list/list/
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.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include "CharacterList.h"

using namespace std;



	/************************************
	* CharacterList Default Constructor *
	*************************************/
	CharacterList::CharacterList()
	{
	}
	/********************************
	*  CharacterList Deconstructor  *
	*********************************/
	CharacterList::~CharacterList()
	{
	}
	/**********************************
	* CharacterList Add New Character *
	***********************************/
	bool CharacterList::addCharacter(Character *newCharacter)
	{
/*error @ -> */		Character *m_pHead -> Character *m_pNext -> Character *m_pNext -> Character *m_pNext = NULL;
		if(m_pHead==NULL)
		{
			return false;
		}

		else
		{

		}
/*error at back*/		back = NULL;
/*error at temp*/		temp = m_pHead;

		while((temp != NULL)&&(strcmp(temp->getName(), ch->getName)<0))//alphabetize names
		{
			
		}
	}
	/***********************************
	*  CharacterList Delete Character  *
	************************************/
	Character CharacterList::*deleteCharacter(char *characterName)
	{
/*error @ temp and name*/		(temp!=NULL) && (strcmp(name, temp->getName)!=0);//searching for character
	}
	/***************************************
	* CharacterList Add New Character Item *
	****************************************/
	bool CharacterList::addItem(char *characterName, Item *newItem)
	{

	}
	/*********************************
	*     CharacterList Get Item     *
	**********************************/
	Item CharacterList::*getItem(char *characterName, char *itemName)
	{

	}
	/**********************************
	*     CharacterList Drop Item     *
	***********************************/
	Item CharacterList::*dropItem(char *characterName, char *itemName)
	{

	}
	/***********************************
	*  CharacterList Print Characters  *
	************************************/
	void CharacterList::printCharacters()
	{
		
	}


Obviously not all code has been written yet.
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.
I don't understand what you're trying to do at line 26. Can you clarify what it is you're trying to do there?
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.
Weird. I don't know why it would be different on your phone. Line 26 is:

/*error @ -> */ Character *m_pHead -> Character *m_pNext -> Character *m_pNext -> Character *m_pNext = NULL;
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.
so here we go again... please review and tell me what I'm doing wrong...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <string>
#include "CharacterList.h"
#include "Character.h"

using namespace std;

	/************************************
	* CharacterList Default Constructor *
	*************************************/
	CharacterList::CharacterList()
	{
		m_pHead = NULL;
	}
	/********************************
	*  CharacterList Deconstructor  *
	*********************************/
	CharacterList::~CharacterList()
	{
	}
	/**********************************
	* CharacterList Add New Character *
	***********************************/
	bool CharacterList::addCharacter(Character *newCharacter)
	{
		CharacterList *temp, *newCharacter;
		newCharacter = new Character();

		if(newCharacter == NULL)
		{
			return false;
		}

/*error @ * and then characterName after = */		newCharacter -> *characterName = characterName;
		else
		{
			
		}

		while((temp != NULL)&&(strcmp(temp->getName(), ch->getName)<0))//alphabetize names
		{
			

		}
	}
	/***********************************
	*  CharacterList Delete Character  *
	************************************/
	Character CharacterList::*deleteCharacter(char *characterName)
	{
		Character *temp;
/*eorr @ m_pHead*/		Character temp = m_pHead;

/*error @ name*/		while((temp!=NULL) && (strcmp(*name, temp->getName)!=0)) //searching for character
		{
/* error @ temp after = */			CharacterList temp = temp -> m_pNext;
		}

		if(temp == NULL)
			return false;
		else

		
	}
	/***************************************
	* CharacterList Add New Character Item *
	****************************************/
	bool CharacterList::addItem(char *characterName, Item *newItem)
	{
		Item *temp, *newItem;

		newItem = new Item();
		if(newItem == NULL)
			return false;
		else
			*newItem -> m_sItemName = *characterName;
			newItem -> m_dValue = *characterName;
			newItem -> m_dWeight = *characterName;
			newItem -> m_iType = *characterName;
/*error @ ->*/			Character newItem -> m_pNext = NULL;

		
	}
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.

CharacterList.cpp (with annotated errors, again my apologies)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <string>
#include "CharacterList.h"
#include "Character.h"

using namespace std;

	/************************************
	* CharacterList Default Constructor *
	*************************************/
	CharacterList::CharacterList()
	{
		m_pHead = NULL;
	}
	/********************************
	*  CharacterList Deconstructor  *
	*********************************/
	CharacterList::~CharacterList()
	{
	}
	/**********************************
	* CharacterList Add New Character *
	***********************************/
	bool CharacterList::addCharacter(Character *newCharacter)
	{
		CharacterList *temp, *newCharacter;
		newCharacter = new Character();

		if(newCharacter == NULL)
		{
			return false;
		}

		newCharacter -> *characterName = characterName; //expected a member name, and characterName undefined
		else
		{
			
		}

		while((temp != NULL)&&(strcmp(temp->getName(), ch->getName)<0))//alphabetize names
		{
			

		}
	}
	/***********************************
	*  CharacterList Delete Character  *
	************************************/
	Character CharacterList::*deleteCharacter(char *characterName)
	{
		Character *temp;
		Character temp = m_pHead; //m_phead undefined

		while((temp!=NULL) && (strcmp(*name, temp->getName)!=0)) //name undefined
		{
			CharacterList temp = temp -> m_pNext; //expression must have pointer type
		}

		if(temp == NULL)
			return false;
		else

		
	}
	/***************************************
	* CharacterList Add New Character Item *
	****************************************/
	bool CharacterList::addItem(char *characterName, Item *newItem)
	{
		Item *temp, *newItem;

		newItem = new Item();
		if(newItem == NULL)
			return false;
		else
			*newItem -> m_sItemName = *characterName;
			newItem -> m_dValue = *characterName;
			newItem -> m_dWeight = *characterName;
			newItem -> m_iType = *characterName;
			Character newItem -> m_pNext = NULL; //expected a ; 
Character.cpp and Character.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include <string>
#include "Character.h"
#ifndef ITEM_H
#define ITEM_H

using namespace std;
	/********************************
	*  Define Character Constructor *
	*********************************/
	Character::Character(char *name, int cl, int al, int hp, int str, int dex, int con, int itl, int wis, int chr)
	{
		strcpy(m_sName, name);
		m_iClass = cl;
		m_iAlignment = al;
		m_iHitPoints = hp;
		m_iCharTraits[0] = str;
		m_iCharTraits[1] = dex;
		m_iCharTraits[2] = con;
		m_iCharTraits[3] = itl;
		m_iCharTraits[4] = wis;
		m_iCharTraits[5] = chr;
	}
	/********************************
	* Character Default Constructor *
	*********************************/
	Character::Character()
	{
		for(int i=0; i<10; i++)
		{
			strcpy(m_Items[i].m_sItemName, "---");
		}
	}
	/********************************
	*   Character Deconstructor     *
	*********************************/
	Character::~Character()
	{
	}

	/********************************
	*       Get Player Name         *
	*********************************/
	int Character::getName(char *name)
	{
		strcpy(name, m_sName);
		return *name;
	}
	/********************************
	*       Set Player Name         *
	*********************************/
	void Character::setName(char *name) 
	{
		 strcpy(m_sName, name);
	}
	/********************************
	*       Get Player Class        *
	*********************************/
	int Character::getClass(int &cl)
	{
		cl = m_iClass;
		return cl;
	}
	/********************************
	*       Set Player Class        *
	*********************************/
	void Character::setClass(int cl)
	{
		m_iClass = cl;
	}
	/********************************
	*     Get Player Alignment      *
	*********************************/
	int Character::getAlignment(int &al)
	{
		al = m_iAlignment;
		return al;
	}
	/********************************
	*     Set Player Alignment      *
	*********************************/
	void Character::setAlignment(int al) 
	{
		m_iAlignment = al;
	}
	/********************************
	*     Get Player Hit Points     *
	*********************************/
	int Character::getHitPoints(int &hp)
	{
		 hp = m_iHitPoints;
		 return hp;
	}
	/********************************
	*     Set Player Hit Points     *
	*********************************/
	void Character::setHitPoints(int hp)
	{
		m_iHitPoints = hp;
	}
	/********************************
	*     Get Player Strength       *
	*********************************/
	int Character::getStrength(int *str)
	{
		*str = m_iCharTraits[0];
		return *str;
	}
	/********************************
	*     Set Player Strength       *
	*********************************/
	void Character::setStrength(int str)
	{
		m_iCharTraits[0] = str;
	}
	/********************************
	*     Get Player Dexterity      *
	*********************************/
	int Character::getDexterity(int *dex)
	{
		*dex = m_iCharTraits[1];
		return *dex;
	}
	/********************************
	*     Set Player Dexterity      *
	*********************************/
	void Character::setDexterity(int dex) 
	{
		m_iCharTraits[1] = dex;
	}
	/********************************
	*     Get Player Constitution   *
	*********************************/
	int Character::getConstitution(int *con)
	{
		*con = m_iCharTraits[2];
		return *con;
	}
	/********************************
	*     Set Player Constitution   *
	*********************************/
	void Character::setConstitution(int con) 
	{
		m_iCharTraits[2] = con;
	}
	/********************************
	*    Get Player Intelligence    *
	*********************************/
	int Character::getIntelligence(int *itl)
	{
		*itl = m_iCharTraits[3];
		return *itl;
	}
	/********************************
	*    Set Player Intelligence    *
	*********************************/
	void Character::setIntelligence(int itl) 
	{
		m_iCharTraits[3] = itl;
	}
	/********************************
	*      Get Player Wisdom        *
	*********************************/
	int Character::getWisdom(int *wis)
	{
		*wis = m_iCharTraits[4];
		return *wis;
	}
	/********************************
	*      Set Player Wisdom        *
	*********************************/
	void Character::setWisdom(int wis)
	{
		m_iCharTraits[4] = wis;
	}
	/********************************
	*      Get Player Charisma      *
	*********************************/
	int Character::getCharisma(int *chr)
	{
		*chr = m_iCharTraits[5];
		return *chr;
	}
	/********************************
	*      Set Player Charisma      *
	*********************************/
	void Character::setCharisma(int chr) 
	{
		m_iCharTraits[5] = chr;
	}
	/********************************
	*     Add Item to Character     *
	*********************************/
	bool Character::addItem(Item *item)
	{
    //Look for empty space
    for(int i=0; i<10; i++)
    {
        if(strcmp(m_Items[i].m_sItemName, "---") == 0)
        {
            // Found an empty slot so copy this item here
            m_Items[i] = *item;
            return true;
        }
    }
    //Item array is full
    return false;
	}
	/********************************
	*       Get Item(Pointer)       *
	*********************************/
	Item *Character::getItem(char *itemName)
	{
		for(int i=0; i<10; i++)
		{
			if(strcmp(m_Items[i].m_sItemName, itemName) == 0)
				return &m_Items[i];
		}
	return NULL; // Didn't find
	}
	/********************************
	*           Drop Item           *
	*********************************/
	Item *Character::dropItem(char *itemName)
	{
		// Search all items
		for(int i=0; i<10; i++)
		{
			// If this it the correct item name
			if(strcmp(m_Items[i].m_sItemName, itemName) == 0)
			{
				Item *newItem = new Item();
				*newItem = m_Items[i];
				// Set the name of the item to the "Empty" string
				strcpy(m_Items[i].m_sItemName, "---");
				return newItem;
			}
    }
    return NULL;
}
	/********************************
	*      Display Everything       *
	*********************************/
	void Character::printAll() 
	{
		cout << "Name: " << m_sName << "  Class: " << m_iClass << "  Alignment: " << m_iAlignment << "  Hit Points: " << m_iHitPoints;
		cout << "  Strength: " << m_iCharTraits[0] << "  Dexterity: " << m_iCharTraits[1] << "  Constitution: " <<  m_iCharTraits[2];
		cout << "  Intelligence: " << m_iCharTraits[3] << "  Wisdom: " << m_iCharTraits[4] << "  Charisma: " << m_iCharTraits[5] << endl;
	}
#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#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 
Item.h and CharacterList.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef ITEM_H
#define ITEM_H

struct Item
{
    char    m_sItemName[65];
    int     m_iType;
    double  m_dValue;
    double  m_dWeight;

};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Character.h"
#include "Item.h"

class CharacterList
{
 private:
	 Character *m_pHead;

public:
	CharacterList();
	~CharacterList();
	bool addCharacter(Character *newCharacter);
	Character *deleteCharacter(char *characterName);
	bool addItem(char *characterName, Item *newItem);
	Item *getItem(char *characterName, char *itemName);
	Item *dropItem(char *characterName, char *itemName);
	void printCharacters();
};
Last edited on
1
2
3
4
5
*newItem -> m_sItemName = *characterName;
			newItem -> m_dValue = *characterName;
			newItem -> m_dWeight = *characterName;
			newItem -> m_iType = *characterName;
			Character newItem -> m_pNext = NULL; //expected a ; 


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.


Last edited on
Topic archived. No new replies allowed.