【问题标题】:C Error C2371 redefinitionC 错误 C2371 重新定义
【发布时间】:2012-12-16 22:41:56
【问题描述】:
Error 2 error C2371: 'QixC': redefinition; different basic types line 5
Error 5 error C2371: 'QixC': redefinition; different basic types line 5
Error 13 error C2371: 'QixC': redefinition; different basic types line 5

这是文件 Game.h 的一部分:

#include "Graphics_Console.h"
#include <stdio.h>
#include <conio.h>

typedef struct
{
int X;
int Y;
}QixC;
typedef struct
{
int X;
int Y;
}CCursor;

我在 Game.c 中使用它们:

#include "Game.h"
#include <stdio.h>
#include <conio.h>
#include <time.h>
int QIX(int nivell)
{
QixC Qix;
CCursor Cursor;
HANDLE hScreen;
int DirQixX,DirQixY;
int IniciX,IniciY;
int FiX, FiY;
int DirStix=0;
int Area=0,AreaTotal=0,AreaObjectiu=(FI_X-INICI_X)*(FI_Y-INICI_Y)*0.75;
int Pantalla=1;
int Puntuacio=0;
int tecla=0;
int viu=1;
int NCops=0, Velocitat=1000/(nivell*0.70);
int XocStix=0;
char continuar[1];

hScreen = GetStdHandle(STD_OUTPUT_HANDLE);
InitScreen(hScreen);
do
{
    system("CLS");
    IniciX=INICI_X,IniciY=INICI_Y;
    FiX=FI_X, FiY=FI_Y;
    Cursor.X=INICI_X+(FiX-INICI_Y)/2,Cursor.Y=FI_Y;

    DibuixarRectangle(IniciX,IniciY,FI_X,FI_Y,hScreen);
    InfoPuntsPartida(hScreen, Puntuacio);
    InfoPantallaPartida(hScreen, Pantalla);

    Qix.X=Aleatori(IniciX+1,FiX-1),Qix.Y=Aleatori(IniciY+1,FiY-1);
    InicialitzarDirQix(&DirQixX,&DirQixY);
    PintarQix(Qix,hScreen);
    PintarCursor(Cursor.X,Cursor.Y,hScreen);

    do{
        if(_kbhit())
        {   
            tecla=LlegirEvent();
            TractarEvent(tecla,Cursor,&IniciX,&IniciY,&FiX,&FiY,Qix,&DirStix,&Area,hScreen);
            if(Area)
            {
                Puntuacio=Puntuacio+Area;
                AreaTotal+=Area;
                Area=0;
                InfoPuntsPartida(hScreen,Puntuacio);
            }
        }
        NCops++;
        if(NCops==Velocitat)
        { 
            if(DirStix!=0)
                XocStix=QiXXocStiX(Qix,Cursor,DirStix);
            if(!XocStix)
                MoureQix(Qix,&DirQixX,&DirQixY,IniciX,IniciY,FiX,FiY,hScreen);
            else
                viu=0;
            NCops=0;
        }
    }while((tecla!=TECLA_q)&&(tecla!=TECLA_Q)&&viu &&(AreaTotal<AreaObjectiu));
    GameOver(hScreen);
    TextColor(LIGHTGREY,BLACK,hScreen);
    GotoXY(0,FI_Y+1,hScreen);
    return Puntuacio;
    system ("PAUSE");
    printf("Continuar?(s/n)");
    scanf("%c",&continuar);
}while(continuar!="s");
}

对不起,外来词和名字,英语不是我的第一语言。 我看不到在哪里重新定义。它们不会出现在其他任何地方,而是作为参数传递给某些函数。请帮忙?

【问题讨论】:

  • 你在使用标题保护吗?
  • Graphics_Console.h 标头中有什么?

标签: c


【解决方案1】:

尝试保护您的标题不被多次包含。

#ifndef GAME_H
#define GAME_H

#include "Graphics_Console.h"
#include <stdio.h>
#include <conio.h>

typedef struct
{
    int X;
    int Y;
}QixC;

typedef struct
{
    int X;
    int Y;
}CCursor;

#endif /* GAME_H */

默认情况下,如果您包含多个直接或间接包含您的标题的标题,您将获得其结构和功能的多个竞争(如果相同)版本。您可以通过启动标题来避免这种情况

#ifndef SOMETHING_UNIQUE_TO_YOUR_HEADER
#define SOMETHING_UNIQUE_TO_YOUR_HEADER

然后结束

#endif /* SOMETHING_UNIQUE_TO_YOUR_HEADER */

保证任何源文件最多包含一个结构副本。

【讨论】:

  • @DavidHeffernan: #pragma once 是非标准的,如果编译器由于某种原因无法识别两个名称引用同一个文件,则可能会导致问题。
  • 非常感谢,看来我的“#include”太多了,程序就好像我一遍又一遍地定义了结构。
  • @EricErañaMouro 很高兴它有帮助。请注意,虽然减少包含的数量可能对您有用,但不适用于更大、更复杂的项目。养成使用#ifndef 保护开始/结束每个头文件的习惯会很好
  • @Keith True。但#pragma once 也有优势。
猜你喜欢
  • 2010-09-07
  • 2016-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
相关资源
最近更新 更多