Home > Mobile >  Error: " expected unqualified-id before 'int' ", array, argument, function,
Error: " expected unqualified-id before 'int' ", array, argument, function,

Time:01-23

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

}


CodePudding user response:

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:

  1. We don't need to pass size of the vector as an argument
  2. the vector is passed by reference to avoid copying

The output of the above program can be seen here.

CodePudding user response:

You are not asking a question, only mentioning an error.
So here is how to avoid that error:

void display_current_array(int array[size][size], int size)
{}

Of course, it gets you another error:

error: 'size' was not declared in this scope
 void display_current_array(int array[size][size], int size)

Which is what πάντα ῥεῖ hinted at in comments.

So, avoid the variable size array attempt.

void display_current_array(int array[][], int size)
{}

which gets you

main.cpp:5:40: error: declaration of 'array' as multidimensional array must have bounds for all dimensions except the first
 void display_current_array(int array[][], int size)

So... insert one size again? No, for the same reason it was removed. You can get rid of that error:

void display_current_array(int array[][42], int size)
{}

But it is a safe guess that it is exactly what you do not want (a constant size).

So we get to the comment by BoP, that you should (or maybe even tried to) use std::array. Once that (in contrast to try C-style variable length arrays, which is not allowed in C ) is clearly decided, just find a tutorial for the details.

CodePudding user response:

A much better way to write this code is using the STL vector container. If you would like to do it with arrays, you need to understand pointers.

#include <iostream>
#include <stdlib.h>
#include <vector>
using namespace std;



// a void function to display the array after every moove
void display_current_array(vector<vector<int>> &array, int size){ // & means pass-by-reference

    
    //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
    vector<vector<int>> array;


    // 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  )
    {
        vector<int> row;
        for (int j = 0; j < size; j  )
        {
            row.push_back(0);
        }
        array.push_back(row);
        
    }

    display_current_array(array, size);

}

Here, I passed the vector by reference using the & operand.

void display_current_array(vector<vector<int>> &array, int size)
  •  Tags:  
  • Related