c - 错误: invalid type argument of '->' (have 'Personaggio' )

标签 c struct compiler-errors

我在创建一个在 16x16 矩阵上移动机器人的函数时遇到这个问题,我想用 ladro 的坐标 Y 减去 Guardia 的坐标 x 。

下面是代码:

`

    typedef struct{
    int  nx;
    int  ny;
    }Personaggio;
Personaggio Ladro;
Personaggio Guardia;

void moveguardia(char scacchiera [][16], Personaggio *Guardia)

    {
          int movimento, GxMed, GyMed;
          if(scacchiera[Guardia->nx-1][Guardia->ny]=='m'){
            movimento=rand()%4;
            if(movimento==0){
              Guardia->nx=Guardia->nx+1;
              scacchiera[Guardia->nx-1][Guardia->ny]=' ';
              scacchiera[Guardia->nx][Guardia->ny]='G';
            }
        else if(movimento==1){
               Guardia->nx=Guardia->nx-1;
               scacchiera[Guardia->nx+1][Guardia->ny]=' ';
               scacchiera[Guardia->nx][Guardia->ny]='G';
        }
        else   if(movimento==2){
               Guardia->ny=Guardia->ny-1;
               scacchiera[Guardia->nx][Guardia->ny+1]=' ';
               scacchiera[Guardia->nx][Guardia->ny]='G';
        }
        else   if(movimento==3){
               Guardia->ny=Guardia->ny+1;
               scacchiera[Guardia->nx][Guardia->ny-1]=' ';
               scacchiera[Guardia->nx][Guardia->ny]='G';
        }
            }
        GxMed=abs((Guardia->nx) - (Ladro->nx));//error

错误在此行 GxMed=abs((Guardia->nx) - (Ladro->nx));//错误

请查看注释为错误的行。我进行了很多搜索,也尝试将 -> 替换为 。但没用。`

最佳答案

函数内部的变量不知道函数外部的变量。明显的问题似乎是您在函数外部初始化了 Personaggio Ladro。您的函数不知道 Ladro 的存在,因为您没有在函数内部初始化这样的对象。

关于c - 错误: invalid type argument of '->' (have 'Personaggio' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684944/

相关文章:

c - 通过结构将整个数组分配给 C 中的另一个数组

c++ - 结构和数组的初始值设定项太多

c - 具有两个空指针的结构的大小是 4?

c - 程序读取文件最后一行两次

c - STM32f105 BLT指令重定位局部变量

c - 如果 srand() 采用 32 位整数... 为什么 rand() 不返回相同值?

c - 如何在嵌入式程序的开头给出从固定内存区域读取的一些变量 "constant status"?

C++:未使用函数返回值时如何触发编译器错误?

c++ - 给定一个 lambda 捕获,什么规则决定了结果闭包成员的类型?

linker - ifort在Linux上的编译器选项