A
A
Alexander2020-12-09 17:15:21
C++ / C#
Alexander, 2020-12-09 17:15:21

How to fix "expression must have pointer-to-object type" error?

I am making a multi-file C++ project with 2 .cpp files and 1 .h file (direct.h). Here is the main function:

#include <iostream>
#include <direct.h>

using namespace std;

int direct(const int rows, const int cols, int ARR); 

int main()
{
const int rows = 9;
const int cols = 9;

int ARR[rows][cols];

direct(const int rows, const int cols, int ARR);

}


And here is my function that fills the array in a spiral:

int direct(const int rows, const int cols, int ARR)
{
int val;

// Initialize the array to 0 values
for (int i = 0; i < rows;i++)
{
    for (int j = 0; j < cols;j++) 
    {
        val = 0;
    }
}

// Use symbols for directions
enum dir
{
    left = 0,
    down,
    up,
    right,
}
dir = left;

// Define the starting point and starting value
int x = rows / 2;
int y = cols / 2;
int val = 1;

// A flag to know when to stop
bool stop = false;

// Start
for (;;)
{
    ARR[x][y] = val++;
    switch (dir)
    {
    case left:
        y -= 1;
        if (y < 0) stop = true;
        else if (ARR[x + 1][y] == 0) dir = down;
        break;
    case down:
        x += 1;
        if (x > rows) stop = true;
        else if (ARR[x][y + 1] == 0) dir = right;
        break;
    case right:
        y += 1;
        if (y >= rows) stop = true;
        else if (ARR[x - 1][y] == 0) dir = up;
        break;
    case up:
        x -= 1;
        if (x < 0) stop = true;
        else if (ARR[x][y - 1] == 0) dir = left;
    }
    if (stop) break;
}
}


Everything is fine in the main.cpp file. However, in the direct.cpp file (my function) it throws "expression must have pointer-to-object type" errors and the "x" and "y" variables inside the "for" loop are underlined in red, so those are the problem.
What am I doing wrong? And how can I fix it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Ananiev, 2020-12-09
@AlexB_49

It should be like this

int direct(const int rows, const int cols, int** ARR);

But in c++ it's better to use std::vector

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question