[英]How can I fix the error “expression must have pointer-to-object type” (multifile project)?
I'm writing the program with 2.cpp files and 1.h file (direct.h).我正在用 2.cpp 文件和 1.h 文件(direct.h)编写程序。 The main function is:
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);
}
My function (that fills the array in the spiral way) is:我的 function (以螺旋方式填充阵列)是:
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;
}
}
In the main.cpp file everything is OK.在 main.cpp 文件中一切正常。 However, in direct.cpp file I get the errors that say "expression must have pointer-to-object type" and the variables "x" and "y" are underlined in the loop "for", so the problem is in them.
但是,在 direct.cpp 文件中,我收到“表达式必须具有指向对象类型的指针”的错误消息,并且变量“x”和“y”在“for”循环中带有下划线,因此问题出在其中。
What am I doing wrong?我究竟做错了什么? And how can I fix it?
我该如何解决?
Start to Replace开始更换
int direct(const int rows, const int cols, int ARR)
by经过
int direct(const int rows, const int cols, int ARR[][])
in the main主要是
call the function:致电 function:
direct( rows, cols, ARR);
When you init the tab:当您初始化选项卡时:
for (int i = 0; i < rows;i++)
{
for (int j = 0; j < cols;j++)
{
ARR[i][j]= 0;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.