在C typedef结构未知类型名 [英] Unknown type name with typedef struct in C

查看:330
本文介绍了在C typedef结构未知类型名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code被写入在一个名为Monster.c文件。我有Monster.h在我的两个GameMain.c文件和我的Monster.c文件中定义。在Monster.h内部文件中的code是:

This code is written in a file named Monster.c. I have Monster.h defined in both my GameMain.c file and my Monster.c file. Inside the Monster.h file the code is:

#ifdef MONSTER_H_
#define MONSTER_H_

int Monster(int Selection);
EnemyStats MonsterStats(int Selection);

#endif

我遂作出的Monster.c文件我typedef结构并建立返回值的结构功能。

I then made my typedef struct in the Monster.c file and created the function to return the values for the struct.

typedef struct EnemyStats
{
    int EnemyHP;
    int VictoryExp;
    int EnemyLevel;
}
EnemyStats;

EnemyStats MonsterStats(int Selection)
{
    struct EnemyStats value;

    switch(Selection)
    {
        case 1:
            value.EnemyLevel = 1;
            value.VictoryExp = 1;
            value.EnemyHP = 1;
            return value;

        case 2:
            value.EnemyLevel = 1;
            value.VictoryExp = 1;
            value.EnemyHP = 1;
            return value;
        ...
    }

在GameMain.c文件,我用这个code,试图访问存储在结构中的信息:

In the GameMain.c file, I used this code to try and access the information stored in the struct:

EnemyStats result;
...
printf("%d", result.EnemyLevel);

这给了我,当我使用gcc GameMain.c Monster.c

It gives me the error when I use gcc GameMain.c Monster.c

GameMain.c:40:2:错误:未知类型名称'EnemyStats

GameMain.c:40:2: error: unknown type name ‘EnemyStats’

GameMain.c:61:25:错误:在一些请求成员的EnemyHP不
  结构或联合

GameMain.c:61:25: error: request for member ‘EnemyHP’ in something not a structure or union

如果我尝试添加结构EnemyStats结果之前;它给了我这个错误。

If I try to add struct before EnemyStats result; it gives me this error.

GameMain.c:在函数'主':

GameMain.c: In function ‘main’:

GameMain.c:40:20:错误:的结果存储大小是不知道

GameMain.c:40:20: error: storage size of ‘result’ isn’t known

我不确定我在做什么错。任何帮助是AP preciated。

I am unsure of what I am doing wrong. Any help is appreciated.

对于任何人谁不知道,我现在用的......说我逃课code,这并不影响我所描述的情况或code是多余的

推荐答案

思考:


  1. 它的 的#ifndef ,不是 #IFDEF 。你想运行code如果 MONSTER_H _ 尚未确定。

  2. 结构体的定义通常去在头文件。

  1. It's #ifndef, not #ifdef. You want to run that code if MONSTER_H_ hasn't been defined yet.
  2. The definition of the struct normally goes in the header file.

把所有这一切在一起,我们有:

Putting all of this together, we have:

#ifndef MONSTER_H_
#define MONSTER_H_

typedef struct EnemyStats {
    int EnemyHP;
    int VictoryExp;
    int EnemyLevel;
} EnemyStats;

int Monster(int Selection);
EnemyStats MonsterStats(int Selection);

#endif

Monster.c

#include "Monster.h"

EnemyStats MonsterStats(int selection)
{
    struct EnemyStats value;
    return value;
}

int Monster(int selection) {
    return 0;
}

GameMain.c

#include <stdio.h>
#include "Monster.h"

int main() {
    EnemyStats result;
    printf("%d", result.EnemyLevel);
}

这篇关于在C typedef结构未知类型名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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