的sizeof无效的应用程序不完全类型与结构 [英] Invalid application of sizeof to incomplete type with a struct

查看:224
本文介绍了的sizeof无效的应用程序不完全类型与结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图做一个游戏,我有一个结构,我把所有关于球员的信息。这是我的结构:

So I'm trying to make a game, and I have a struct where I put all the information about the players. That's my struct:

struct player{
   int startingCapital;
   int currentCapital;
   int startingPosition;
   int currentPosition;
   int activePlayer; 
   int canPlay;      
};

这就是我的主要:

And that's my main:

#include <stdio.h>
#include <stdlib.h>
#include "header.h"


int main(int argc, char *argv[])
{  int s,i,numOfPlayers;
   struct player *players;
    printf("Give the number of players: \n");
    scanf("%d",&numOfPlayers);

    players = (struct player *)calloc(numOfPlayers,sizeof(struct player));


   system("PAUSE"); 
  return 0;
}

因此​​,大家可以看到,我要求用户给出的玩家人数,然后我尝试分配所需的内存。但我发现了,我想不通这个编译器错误。 无效申请的sizeof不完全类型播放器

推荐答案

这意味着包含文件没有访问播放器结构定义(即不知道它是什么样子)。

It means the file containing main doesn't have access to the player structure definition (i.e. doesn't know what it looks like).

尝试将其纳入 header.h 或使一个构造函数一样函数,它分配如果它是一个不透明的对象。

Try including it in header.h or make a constructor-like function that allocates it if it's to be an opaque object.

如果你的目标是隐藏结构的实现,这样做是可以访问该结构的C文件:

If your goal is to hide the implementation of the structure, do this in a C file that has access to the struct:

struct player *
init_player(...)
{
    struct player *p = calloc(1, sizeof *p);

    /* ... */
    return p;
}

但是,如果执行不应该被隐藏 - 即应依法说 P-GT&; canPlay = 1 倒不如把结构定义在 header.h

However if the implementation shouldn't be hidden - i.e. main should legally say p->canPlay = 1 it would be better to put the definition of the structure in header.h.

这篇关于的sizeof无效的应用程序不完全类型与结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆