Need help with expression error.

Pages: 12
Jan 24, 2017 at 5:12am
Im getting an error at line 67 "cout << party_name[x] << "\n";" expression must be a pointer to a complete object type. Any help would be great as I am quite new to C++ and am not sure what exactly is wrong with the code. I have googled a fair bit but nothing has really made sense as to why I am not able to get the string array passed back correctly.

I am a student with the assignment to create a text based game and for part of the criteria we need to randomise the amount of party members that are with you and give the ability to assign them names. I have managed to get the amount of party members done and the variable is being passed back to the game section perfectly fine where I am struggling is passing back the string array party_names into the game function.

I should stress these all come from the main which goes straight into the game function the formatting is just for the programs sake.
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
  #include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

void game();

int party_rnd(int &party_members)
{
	char roll_party;

	cout << "Please [R]oll the dice to decide how many will dare enter with you.\n";

	cin >> roll_party;

	if (roll_party == 'R') {
		srand(time(NULL));
		party_members = rand() % 6 + 1;
		cout << "You have been joined by " << party_members << " of your friends!\n\n";
	}
	else {
		cout << "\nYou can't not roll!\n";
		system("Pause");
		system("cls");
		game();
	}

	return 0;
}

string party_name(int &party_members, string (&party_names))
{
	string* party_name = new string[party_members];
	for (int i = 0; i < party_members; i++) {
		cout << "Please enter a name for your party member: ";
		cin >> party_name[i];
		cout << endl;
	}

	cout << "Your party consists of: ";
	cout << endl;

	for (int x = 0; x < party_members; x++) {
		cout << party_name[x] << "\n";
	}

	return *party_name;
}

void game()
{
	system("cls");

	int party_members = 0;
	string* party_names = new string[party_members];

	cout << "Welcome to Professor Scott and the Temple of Doom!\n";
	cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";

	party_rnd(party_members);
	
	party_name(party_members, party_names[party_members]);

	for (int y = 0; y < party_members; y++) {
		cout << party_name[y] << "\n";
	}

	return;
}
Jan 24, 2017 at 5:35am
closed account (48T7M4Gy)
line 56: party_members = 0
line 57: therefore party_name array has zero elements ...

If you are trying to setup an array with a random number of elements I think you should ensure that there is at least one.
Last edited on Jan 24, 2017 at 5:37am
Jan 24, 2017 at 5:39am
Thank you for the reply, I have changed that line to: int party_members; instead of having a = value. However the error is there still even with this change.
Jan 24, 2017 at 5:46am
closed account (48T7M4Gy)
1
2
3
4
5
6
int party_members = 0;
    party_rnd(party_members); // *** <---
    string* party_names = new string[party_members];
    
    cout << "Welcome to Professor Scott and the Temple of Doom!\n";
    cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";


Maybe this is what you meant so you get a random number of players. In other words reposition the arrowed line.
Last edited on Jan 24, 2017 at 5:48am
Jan 24, 2017 at 5:51am
The issue isn't with the random party members it is with getting the array of the party members back into the game function with their names.
expression must be a pointer to a complete object type
is the error I am getting at line 67 in the code from the first post.
Jan 24, 2017 at 6:03am
closed account (48T7M4Gy)
Please yourself. I can run your code without error by making the change I suggest.

At the moment you are trying to access an array with no elements after having created one. However, please yourself and good luck in your ventures. :)
Jan 24, 2017 at 6:06am
I did the changes you suggested and was still getting the error on line 67 which is why I didn't see it as fixed. This is the current code I have for void game() where I am still getting that error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void game()
{
	system("cls");

	int party_members;
	party_rnd(party_members);
	string* party_names = new string[party_members];

	cout << "Welcome to Professor Scott and the Temple of Doom!\n";
	cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";

	party_name(party_members, party_names[party_members]);

	for (int y = 0; y < party_members; y++) {
		cout << party_name[y] << "\n";
	}

	return;
}
Jan 24, 2017 at 6:11am
closed account (48T7M4Gy)
OK. Where is main() in your program?

I suggest also you print out the value of party_members.
Last edited on Jan 24, 2017 at 6:17am
Jan 24, 2017 at 6:17am
The main() I didn't include in the quote because I didn't want to paste a huge wall of code but have put all the code I have below. I altered the code to put the party_members value along with each name as it gets printed out.

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
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

void game();

int party_rnd(int &party_members)
{
	char roll_party;

	cout << "Welcome to Professor Scott and the Temple of Doom!\n";
	cout << "Before you can enter you must roll a dice to see how many of your friends will follow you!\n\n";

	cout << "Please [R]oll the dice to decide how many will dare enter with you.\n";

	cin >> roll_party;

	if (roll_party == 'R') {
		srand(time(NULL));
		party_members = rand() % 6 + 1;
		cout << "You have been joined by " << party_members << " of your friends!\n\n";
	}
	else {
		cout << "\nYou can't not roll!\n";
		system("Pause");
		system("cls");
		game();
	}

	return 0;
}

string party_name(int &party_members, string (&party_names))
{
	string* party_name = new string[party_members];
	for (int i = 0; i < party_members; i++) {
		cout << "Please enter a name for your party member: ";
		cin >> party_name[i];
		cout << endl;
	}

	cout << "Your party consists of: ";
	cout << endl;

	for (int x = 0; x < party_members; x++) {
		cout << party_name[x] << " " << party_members << "\n";
	}

	return party_name[party_members];
}

void game()
{
	system("cls");

	int party_members;
	party_rnd(party_members);
	string* party_names = new string[party_members];

	party_name(party_members, party_names[party_members]);

	for (int y = 0; y < party_members; y++) {
		cout << party_name[y] << "\n";
	}
	
	return;
}

int main()
{
	char menu_choice;
	cout << "Main Menu:\n";
	cout << "[S]tart Game\n";
	cout << "[E]xit Game\n\n";

	cout << "Please choose an option: ";
	cin >> menu_choice;

	if (menu_choice == 'S') {
		cout << "\nWelcome to The Temple of Doom!";
		game();
	}
	else if (menu_choice == 'E') {
		cout << "\nMaybe you'll enter next time.";
	} else {
		cout << "\nPlease enter a valid option\n";
		system("Pause");
		system("cls");
		main();
	}
	return 0;
}
Jan 24, 2017 at 6:19am
closed account (48T7M4Gy)
That's better. :)
Jan 24, 2017 at 6:24am
closed account (48T7M4Gy)
line 66 party_names[y] ??
Jan 24, 2017 at 6:29am
Well that fixed the error. Can't believe I missed the 's' off. I changed that it built without any errors however when run after entering the last name it crashes.
Jan 24, 2017 at 6:33am
closed account (48T7M4Gy)
It doesn't help you much but it doesn't crash on my machine. Maybe you need to 'clean and rebuild'.
Jan 24, 2017 at 6:35am
Just did a clean and rebuild and am getting the following:

Debug Error! abort() has been called.

This is after entering the last part member's name.
Jan 24, 2017 at 6:43am
closed account (48T7M4Gy)
This is often an out of bounds error.

Also, because you are using new without a corresponding delete[] there might be a problem when you run the program without exiting - just a guess on my part.
Jan 24, 2017 at 6:45am
So if that is the case what would be the the solution? I am assuming that delete would delete the array and if so I would need to do that at the very end of the program which is still a ways off.
Jan 24, 2017 at 6:50am
closed account (48T7M4Gy)
What names did you type in?
Jan 24, 2017 at 6:54am
I got 3 so I typed in:

Bill
Ted
Marty
Jan 24, 2017 at 7:04am
closed account (48T7M4Gy)
Try commenting out you pause's and cls's
Jan 24, 2017 at 7:06am
closed account (48T7M4Gy)
This is the output I get:
Main Menu:
[S]tart Game
[E]xit Game

Please choose an option: S

Welcome to The Temple of Doom!Welcome to Professor Scott and the Temple of Doom!
Before you can enter you must roll a dice to see how many of your friends will follow you!

Please [R]oll the dice to decide how many will dare enter with you.
R
You have been joined by 2 of your friends!

Please enter a name for your party member: Mark

Please enter a name for your party member: Mary

Your party consists of: 
Mark 2
Mary 2


Program ended with exit code: 0
Pages: 12