-1

I have a function that validates whether certain coordinates are within a matrix and returns true/false depending on the answer:

bool validateNextLocation(char robot, int proposed_row, int proposed_col, char map[7][7]){
    auto const robot_location = World::getRobotLocation(robot);
    int row = robot_location -> first;
    int col = robot_location -> second;

    if (map[proposed_row][col] != '1' || map[row][proposed_col] != '1'){return false;}
    else{return true;}

}

I am trying to use the function in my switch cases:

switch (direction) {
        case 'L': {
            if (World::validateNextLocation(robot, ++robot_location->first, robot_location-> second, char a[7][7])){
                ++robot_location->first;
            }
            else{return -1;}
        }
        break;
        case 'D': {
            if (World::validateNextLocation(robot, robot_location->first, --robot_location->second, char a[7][7])){
                --robot_location->second;
            }
            else{return -1;}
        }
        break;
        case 'R': {
            if (World::validateNextLocation(robot, --robot_location->first, robot_location->second, char a[7][7])){
                --robot_location->first;
            }
            else{return -1;}
        }
        break;
        default: {
            if (World::validateNextLocation(robot, robot_location->first, ++robot_location->second, char a[7][7])){
                ++robot_location->second;
            }
            else{return -1;}
        }
        break;
    }

But the char a[7][7] has a red underline where the error reads:

Expected '(' for function style cast or type construction

I know I'm not missing a bracket but where am I going wrong?

HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
Onur-Andros Ozbek
  • 2,998
  • 2
  • 29
  • 78
  • `++robot_location->first` increments the value inside the `if` statement. Then you increment it again in the body. Consider using `robot_location->first + 1` in the test case, but you can keep `++` in the body. – Aykhan Hagverdili Jun 06 '22 at 07:20
  • @AyxanHaqverdili Thanks Ayxan but what about the issue I'm stuck with? – Onur-Andros Ozbek Jun 06 '22 at 07:34
  • just do "a" instead of "char a[7][7]". You don't specify types there – Aykhan Hagverdili Jun 06 '22 at 07:37
  • Use std::array it is much more clear on how to pass it around... e.g. `bool validateNextLocation(char robot, int proposed_row, int proposed_col, const std::array,7>& map)`. Or if you really want to do oo, create a Map class because in the end you're working with a map (the WHAT) and not an array (the HOW) – Pepijn Kramer Jun 06 '22 at 07:42
  • Ponder this: you call the function with "robot", not with "char robot", don't you ? –  Jun 06 '22 at 08:04
  • @PepijnKramer: much more clear ?? –  Jun 06 '22 at 08:05

2 Answers2

1

Just change

if (World::validateNextLocation(robot, robot_location->first, 
    ++robot_location->second, char a[7][7])){

to

if (World::validateNextLocation(robot, robot_location->first, 
    ++robot_location->second, a)){

Declaring an array, and using an array are two different things, you don't use the same syntax for both. I am assuming that somewhere in your code you do have a proper declaration for a.

That said passing a 7x7 matrix from one function to another seems unlikely to be the right thing to do, but no doubt that will sort itself out in time.

john
  • 85,011
  • 4
  • 57
  • 81
0
if (World::validateNextLocation(robot, ++robot_location->first, robot_location-> second, char a[7][7])){

The char a[7][7] would declare a new variable a as 7x7 matrix.

First you can't declare a variable inside of function arguments, second the variable would uninitialized and thrid expire at the end of the function call. So three reasons this syntax makes no sense.

You have to declare your array before the function call if you don't have it already and then just pass the variable a as argument.

Goswin von Brederlow
  • 11,875
  • 2
  • 24
  • 42