c++ - 从不同类 C++ 访问数组

标签 c++

我正在为学校开发一款名为 LCR 的骰子游戏。我终于得到了要编译的程序,并且我已经制定了游戏例程。我现在坚持从我的 Player 类访问 main 中的数组。特别是 Player::setChips。我可能应该使用指针,但我在那个领域非常粗糙,任何输入都可能很棒。该函数调用正常,输出有效,但我注释掉的行显然不起作用,但给出了我想做什么的想法。谢谢。

这是我在 LCR Game.cpp 中的代码:

    int main()
{

    // Display Game Rules
    Player::directions();

    // Get # of Players
    int currentPlayer = 0;
    srand((unsigned)time(NULL));

    const int numPlayers = setPlayers(); //set number of players


    static Player* players = new Player[numPlayers]; //set up array 

    for (int i = 0; i < numPlayers; i++) //set names and chips for each player
    {
        cout << endl << "Enter player number " << (i + 1) << "'s name: " << endl;

        players[i].setName();
        players[i].chips = 3;

    }
    // start game

    cout << endl << "OK Let's play!" << endl;

    while (winner == false) {

        for (int i = 0; i < numPlayers; i++) {
            // check if player has chips. if not ++i and skip turn
            if (players[i].chips == 0) {
                cout << endl << "Sorry " << players[i].name << " you have no chips, you must skip this turn" << endl;
                ++i;
            }
            cout << endl << players[i].name << " You have " << players[i].chips << " chips " << " press enter to roll the dice" << endl;
            std::cin.ignore();         //wait for keypress
            for (int j = 0; j < players[i].chips || j < 3; j++) {  // player rolls dice up to 3 times or max chips

                Player::setChips();  // call setChips. roll dice & move chips 

                cout << players[i].chips;
                // check for winner after each diceroll
                totalChips = 0;                          // reset chip counter
                for (int k = 0; k < numPlayers; k++) {   //add all chips on table
                    totalChips = totalChips + players[i].chips;
                }

                // check for winner                 

                if (totalChips - players[i].chips == 0) {
                    cout << endl << "Congratulations " << players[i].name << " you win " << players[i].chips << " chips!";
                    winner = true;
                    return 0;
                }

            }
        }
    }





    return 0;
}

以及来自 Player.cpp 的 Player::setChips 拷贝

void Player::setChips()
{

switch (Dice::rollDice()) // roll the dice
{
case 1:

    cout << endl << "You rolled <L> "; // subtract 1 chip from player and add one to left 

    //--players[i].chips;

    //if (i = numPlayers) {
    //  ++players[1].chips;
    //  }
    break;

case 2:  
    cout << endl << "You rolled <C> "; // subtract 1 chip from player
    //--players[i].chips;
    break; 

case 3:  
    cout << endl << "You rolled <R> "; //subrtact 1 chip from player and give to right
    //--players[i].chips;
    //if (i = 1) {
    //  ++players[numPlayers].chips;

    break; 

case 4: 
    cout << endl << "You rolled <*> ";
    break; //do nothing

case 5: 
    cout << endl << "You rolled <*> ";
    break; // do nothing

case 6: 
    cout << endl << "You rolled <*> ";
    break; //do nothing
}
}

最佳答案

是你的setChips函数静态?

a nonstatic member reference must be relative to a specific object

你的错误是因为函数setChipschips 时是静态的不是。

如果你希望你的函数是static , 你需要制作 chips也是静态的。此外,在 .cpp 的顶部,您需要定义 unsigned Players::chips;那么你可以使用 ++chips在你的里面 Player类函数。

如果你不需要你的函数是static , 只需删除 static从你的功能,你仍然可以使用 ++chips在你的里面 Player类功能很好。

关于c++ - 从不同类 C++ 访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50979422/

相关文章:

c++ - 使用 if (? operator) 语句传递带指针的数组

c++ - 初始化 vector 类型的静态类成员

C++11 std::threads 并等待线程完成

c++ header file compiler issue (Sales_item.h) c++ 入门示例

c++ - 接受整数输入并返回字符串的函数

c++ - 关于分离逻辑 (C++) 和 GUI (Qt) 的概念和基本问题

c++ - wxGrid GetSelectedCells 返回空数组

c++ - WaitForSingleObject 卡住应用程序

带有使用 gsl 的 C 函数的 Java JNI。

c++ - 控制台宽度;承担违约或成为……?