Error: " expected unqualified-id before 'int' ", array, argument, function,
In Standard C++ the size of an array must be a compile time constant. So when you wrote:
int size;
cin>> size;
int array[size][size]; //NOT STANDARD C++
The statement array[size][size];
is not standard c++.
Second when you wrote:
void display_current_array(array[size][size], int size){
//...
}
Note in the first parameter of the function display_current_array
you have not specified the type of elements that the array holds. So this will result in the error you're getting.
Solution
A better way to avoid these complications is to use a dynamically sized container like std::vector
as shown below:
#include <iostream>
#include <vector>
// a void function to display the vector after every moove
void display_current_array(const std::vector<std::vector<int>> &vec){//note the vector is passed by reference to avoid copying
for(auto &row: vec)
{
for(auto &col: row)
{
std::cout << col ;
}
std::cout<<std::endl;
}
}
int main(){
int size;
// ask the player to give a board size
std::cout << "What is the size of your Hex Board ? ";
std::cin>> size;
// create the 2D STD::VECTOR with size rows and size columns to represent the board
std::vector<std::vector<int>> vec(size, std::vector<int>(size));
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
//NO NEED TO INITIALIZE EACH INDIVIDUAL CELL IN THE 2D VECTOR SINCE THEY ARE ALREADY INITIALIED TO 0
display_current_array(vec);
}
Note that:
- We don't need to pass size of the vector as an argument
- the vector is passed by reference to avoid copying
The output of the above program can be seen here.