简体   繁体   English

如何修复错误“表达式必须具有指向对象类型”(多文件项目)?

[英]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.

相关问题 error:expression必须具有指向对象的指针类型 - error: expression must have pointer-to-object type 表达式必须具有指向对象的类型 - Expression must have pointer-to-object type 如何修复IntelliSense:表达式在MFC中必须具有指向对象的指针类型? - How to fix IntelliSense: expression must have pointer-to-object type in MFC? C++ 表达式必须具有指向对象的类型 - C++ Expression must have pointer-to-object type 2d数组问题 - 我得到错误:表达式必须具有指针对象类型(opencv,CUDA) - 2d array issues- I get error : expression must have pointer-to-object type (opencv, CUDA) 函数内部的 C++ 错误:表达式必须具有指向对象的类型 - C++ error inside function: expression must have pointer-to-object type 下标要求数组或指针类型表达式必须具有指针到对象的类型 - subscript requires array or pointer type expression must have pointer-to-object type 表达式必须具有指针到对象的类型,下标需要数组或指针的类型 - expression must have pointer-to-object type,subscript requires array or pointer type 从二维向量中创建一维向量的函数(错误:表达式必须具有指向对象的指针类型) - Function to create 1D-vectors out of 2D-vectors (Error: expression must have pointer-to-object type) MSVC - 表达式必须具有指向对象的类型,但它在泛型数组上具有“float”类型? - MSVC - expression must have pointer-to-object type but it has type "float" on generic array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM