在初始化二维数组然后将其发送给函数时遇到一些问题

Having some trouble with initializing a 2D array and then sending it off to a function

我正在制作一个 TicTacToe 程序,我正在尝试使用 OOP 技术。在我的 'Board' class 中,我希望程序能够存储可以赢得一组动作的每种方式。

我希望这可以在这里演示:

Board.h

#pragma once

class Board
{
private: 
    int winningRows[8][3]; //Variable in question

public:
    static const char X = 'X'; //Game piece 'X'
    static const char O = 'O'; //Game piece 'O'
    static const char EMPTY = ' '; //Empty game piece
    static const char TIE = 'T'; //Game is tie
    static const char NOONE = 'N'; //Nobody has won game yet
    static const int numbOfSquares = 9; //Number of squares on the board

    int InitializeWinningCombinations();
    void FindWinner();
};

Board.cpp

 #include "stdafx.h"
 #include "Board.h"

int Board::InitializeWinningCombinations()
{
    /*
    The playing board
    0, 1, 2
    3, 4, 5
    6, 7, 8
    */
    //All possible ways player can win game
    winningRows[8][3] = {
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    };

    //return winnigRows[8][3];
}


void Board::FindWinner()
{
    //I am wanting to get the variable here so I can play around with it later. 
    int winningRows = InitializeWinningCombinations();
}

我可以只在 'FindWinnner' 函数中使用 'winningRows' 变量,但根据我的理解,最好尽可能抽象并将其作为 'Board' 的成员class

感谢您的宝贵时间。

您必须将签名更改为

int** InitializeWinningCombinations();

那么你可以称它为

int** winningRows = InitializeWinningCombinations();
winningRows[8][3] = {
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    };

尝试的数组赋值不是初始化,无法完成。您可以在

这样的构造函数中初始化数组
Board() : winningRows{
        //Horizontal 
        {0, 1, 2},
        {3, 4, 5},
        {6, 7, 8},
        //Vertical
        {0, 3, 6},
        {1, 4, 7},
        {2, 5, 8},
        //Diagonal
        {2, 4, 6},
        {0, 4, 8}
    } {}

Live Example