Reputation: 25
I am fairly new to C++ and currently following a certification to learn this language. I previously only used languages such as Python.
I found similar posts with the same but none could relate to my code.
I have the following code to create a hex game. I am trying to have a simple function to display the board every time a player makes a moove.
I try to keep the code as simple as possible at first (limit the use of pointers and libraries).
I have this error : hex_game.cpp:9:47: error: expected unqualified-id before 'int' 9 | void display_current_array(array[size][size], int size){ |
Below is my code, hope someone could help :
#include <iostream>
#include <stdlib.h>
using namespace std;
#include <vector>
#include <array>
// a void function to display the array after every moove
void display_current_array(array[size][size], int size){
//s=arr.size();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
cout<<array[i][j]<<endl;
}
}
}
int main(){
// the HEX game is a game of size cases represented by an array either filled with a color;
// int size =11; //as asked but we can play on a differnt board
int size;
// ask the player to give a board size
cout << "What is the size of your Hex Board ? ";
cin>> size;
// create the array to represent the board
int array[size][size];
// the player will choose colors that we will store as an enum type
enum colors {BLUE, RED};
// initialize the array: all positions are 0
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
array[i][j]=0;
}
}
display_current_array(array, size);
}
Upvotes: 0
Views: 991
Reputation: 1
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.
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:
The output of the above program can be seen here.
Upvotes: 2