表达式必须是指向完整对象类型C ++的指针

时间:2017-01-24 05:12:37

标签: c++

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

我在第67行和第34行收到错误; cout&lt;&lt; party_name [x]&lt;&lt; &#34; \ n&#34 ;;&#34; expression必须是指向完整对象类型的指针。任何帮助都会很棒,因为我对C ++很陌生并且不确定代码究竟出了什么问题。我已经google了一下,但没有什么真正有意义的,为什么我不能正确传回字符串数组。

1 个答案:

答案 0 :(得分:0)

#include <iostream>
#include <cstdlib>
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_names[y] << "\n";
}

return;
}

我可以编译上面的代码。点击这里: changed-code