C Error C2371 redefinition - c

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
This is the part of the file 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;
And I use them in 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");
}
Sorry for the foreign words and names, english is not my first language.
I can't see where is redefined. They don't appear anywhere else,but are passed as parameters to some functions. Any help, please?

Try guarding your header against multiple inclusion.
#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 */
By default, if you include multiple headers which directly or indirectly include your header, you'll get multiple competing (if identical) versions of its structures and functions. You can avoid this by starting your header
#ifndef SOMETHING_UNIQUE_TO_YOUR_HEADER
#define SOMETHING_UNIQUE_TO_YOUR_HEADER
and ending it
#endif /* SOMETHING_UNIQUE_TO_YOUR_HEADER */
which guarantees that any source file will include at most a single copy of your structures.

Related

C header files without #ifndef still work...why? [duplicate]

This question already has answers here:
Why are typedef identifiers allowed to be declared multiple times?
(3 answers)
Closed 5 years ago.
Why don't I get the error messages?
I have three header files named DataType.h, printInt.h, printStr.h, and one myApp.c.
DataType.h
typedef int Integer;
typedef char String;
printInt.h
#include "DataType.h"
void printInt(Integer);
printInt.c
#include "printInt.h"
#include <stdio.h>
void printInt(Integer number){
printf("%d\n", number);
}
printStr.h
#include "DataType.h"
void printStr(String*);
printStr.c
#include "printStr.h"
#include <stdio.h>
void printStr(String *str){
printf("%s\n", str);
}
myApp.c
#include "printStr.h"
#include "printInt.h"
Integer main(void){
printInt(20);
printStr("hello");
return 0;
}
Clearly, I have included the DataType.h twice, and I did not use #ifndef to avoid redefinition of Integer and String. Please, someone, tell me how I can get the error messages to demonstrate the directive is working properly.
#ifndef __DATATYPE_H
#define __DATATYPE_H
typedef int Integer;
typedef char String;
#endif
Regardless having #ifndef or not, the gcc complier (version 5.4.0) does not generate any error messages. What's wrong?
Definitions of typedef and prototypes of functions can occur as many times as you want. For example:
typedef int lala;
typedef int lala;
void somePrototype();
void somePrototype();
int main() {
return 0;
}
will compile just fine: https://ideone.com/4EjfaR
Try adding the definition of a function to a header file. You will see then that you get a redefinition error and will require a header guard.

What is wrong with this? C

I'm having trouble finding my error. Here is a definition in structures.h
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
And here is in functions.c where I include header and try to use the type book_t
#include "structures.h"
void load_book(book_t *book) {
}
But I get this error
functions.c:10:16: error: unknown type name ‘book_t’
void load_book(book_t *book) {
^
Edits with more code below:
In my main file I order my .h files like so
#include "structures.h"
#include "functions.h"
structures.h
#ifndef STRUCTURES_H
# define STRUCTURES_H
typedef struct bank_account {
char *name;
int amount;
} bank_account_t;
typedef struct transaction {
char *name;
int amount;
} transaction_t;
typedef struct book {
bank_account_t **accounts;
transaction_t **transactions;
} book_t;
#endif
function.c
#include <stdio.h>
#include "functions.h"
#include "structures.h"
#include "bank_account.h"
#include "transaction.h"
void load_book(book_t *book) {
}
void init_book() {
}
bank_account.h
#ifndef BANK_ACCOUNT_H
# define BANK_ACCOUNT_H
void init_new_bank();
void deinit_new_bank();
#endif
transaction.h
#ifndef TRANSACTION_H
# define TRANSACTION_H
#endif
I think the problem must be in functions.h (which is not included in the original post).
functions.h
#ifndef FUNCTIONS_H
# define FUNCTIONS_H
/* [MarkU] required: include definition of book_t */
#include "structures.h"
void load_book(book_t *book);
void init_book();
#endif
Without the #include structures.h there is no definition of the boot_t type.
Built and verified with mingw32-gcc 4.7.2. Omitting the #include, I see the error message.
In functions.c change the order of those:
#include "functions.h"
#include "structures.h"
to be
#include "structures.h"
#include "functions.h"
The subtile thing is that the error message origins from functions.c not from functions.h.
Assuming the protoytpe to load_book(book_t *) in functions.h, it needs to know about book_t.
So the optimal solution to this would be to include structures.h into functions.h (as also already pointed out by MarkU's answer).
Lesson learned: Always (and only) include what you need and where you need it. Avoid (subtile) dependencies.

error: expected identifier or '(' before 'typedef'|

i'm working since a few weeks on the same project and never had this kind or error.
I got it now without touching to the concerned file, which is down there:
#ifndef DIJKSTRA_H_INCLUDED
#define DIJKSTRA_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINI 100000.0
typedef struct dij
{
int ordre;
float** adjacencePoids;
float* l;
int* pred;
}t_dij;
int choix_action();
int choix_sommet_depart();
int choix_sommet_arrivee();
t_dij* allouer_dijkstra();
t_dij* allouer_dijktra_durees();
t_dij* dijkstra();
void afficher_resultat();
void sauver_resultat();
void detruire_struc();
#endif // DIJKSTRA_H_INCLUDED
This code gives me this error (Compiler is MinGW)
Line 11 error: expected identifier or '(' before 'typedef'|
Thanks!
I suspect the error lies in the file the header shown is included from, or in a file included just before this one.

How to solve Undefined Struct in C?

I've been at this for several days now, unable to compile successfully. I made a very-oversimplified project separate from my main one. Depending which embeded struct I comment out, I get one of the following errors:
file: x2d_gfx_speech_balloon.h
error C2079: 'sprBalloon' uses undefined struct 'X2D_SPRITE'
error C2079: 'font' uses undefined struct 'X2D_FONT'
Side note: Putting the sprite and font headers from the common header into the speech balloon header itself has no effect either.
I'm going to provide the entire simplified code below. Please let me know if you see any syntax wrong, and optionally, feel free to compile it on your side if it helps. My environment is Windows 7 with Visual Studio 2010 and using C89.
main.c
#include "common.h"
int main(void)
{
return 0;
}
common.h
#ifndef _COMMON_H_
#define _COMMON_H_
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
#include "X2D_GFX_Sprite.h"
#include "X2D_GFX_Font.h"
#include "X2D_GFX_Speech_Balloon.h"
#endif
constants.h
#ifndef _CONSTANTS_H_
#define _CONSTANTS_H_
/* General Constants */
#define TRUE 1
#define FALSE 0
#endif
X2D_GFX_Font.h
#ifndef _X2D_GFX_FONT_H_
#define _X2D_GFX_FONT_H_
#include "common.h"
/* Font Structure */
struct X2D_FONT
{
float x, y;
int size;
int blnShow;
};
/* Font Prototypes (not shown here)... */
int fnt_init(struct X2D_FONT *objFont, const char *strFileName,
const char *strText, const float x, const float y,
const unsigned int size);
#endif
X2D_GFX_Font.c
#include "X2D_GFX_Font.h"
int fnt_init(struct X2D_FONT *objFont, const char *strFileName,
const char *strText, const float x, const float y,
const unsigned int size)
{
return TRUE;
}
X2D_GFX_Sprite.h
#ifndef _X2D_GFX_SPRITE_H_
#define _X2D_GFX_SPRITE_H_
#define MAX_VARSI 10
#include "common.h"
struct X2D_SPRITE
{
float x;
float y;
unsigned int width;
unsigned int height;
int blnShow;
float vx, vy;
float angle;
int varsi[MAX_VARSI];
};
int spr_init(struct X2D_SPRITE *spr, const char *strFileName);
#endif
X2D_GFX_Sprite.c
#include "X2D_GFX_Sprite.h"
int spr_init(struct X2D_SPRITE *spr, const char *strFileName)
{
return TRUE;
}
X2D_GFX_SPEECH_BALLOON.h
#ifndef _X2D_GFX_SPEECH_BALLOON_H_
#define _X2D_GFX_SPEECH_BALLOON_H_
#include "common.h"
/* Contains a list of acceptable balloon types */
enum ESpeechBalloonType
{
ESpeechBalloonType_Talk,
ESpeechBalloonType_Thought,
ESpeechBalloonType_Yell,
ESpeechBalloonType_Whisper
};
/* Speech Balloon types */
struct X2D_SPEECH_BALLOON
{
struct X2D_SPRITE sprBalloon;
struct X2D_FONT font;
enum ESpeechBalloonType eBalloonType;
};
#endif
X2D_GFX_SPEECH_BUBBLE.c
#include "X2D_GFX_SPEECH_BALLOON.h"
Your issue is the order in which files are included, specifically in X2D_GFX_Sprite.c. If you run just the preprocessor on that file (in Linux you can use cpp or gcc -E, I don't know how in Windows) and scroll down to the very end, you'll find that the X2D_SPRITE struct is being declared after it's used:
struct X2D_SPEECH_BALLOON
{
struct X2D_SPRITE sprBalloon;
struct X2D_FONT font;
enum ESpeechBalloonType eBalloonType;
};
# 11 "common.h" 2
# 7 "X2D_GFX_Sprite.h" 2
struct X2D_SPRITE
{
float x;
float y;
unsigned int width;
unsigned int height;
int blnShow;
float vx, vy;
float angle;
int varsi[10];
};
Two easiest ways to fix it would be to either not use common.h or move #include "common.h" after the struct declaration in X2D_GFX_Sprite.h.
This happens because X2D_GFX_Sprite.c includes X2D_GFX_Sprite.h, which includes common.h, which includes the other .h files. The preprocessor first copies in X2D_GFX_Sprite.h. When it finds #include "common.h", it begins copying that file. The first three includes are copied in. When it gets back to X2D_GFX_Sprite.h, it'll copy it in, but the header guards will get rid of everything in it. Next, it copies in X2D_GFX_Font.h and X2D_GFX_Speech_Balloon.h, which includes the X2D_SPEECH_BALLOON struct, in that order. Only then does it finally add in the rest of X2D_GFX_Sprite.h, including the X2D_SPRITE struct. This results in the two structs not being copied in the correct order.

Multiple Definition of a function in C, Prototyping

Eclipse tells me that I have mutliple Definitions of a function.
I just can't spot the mistake.
This is my main.c
#include <stdio.h>
#include "kontaktverzeichnis.h"
int main(){
kontakt_hinzufuegen();
return 0;
}
This is the header:
#ifndef KONTAKTVERZEICHNIS_H_
#define KONTAKTVERZEICHNIS_H_
#include "kontaktfunktionen.c"
int kontakt_hinzufuegen();
#endif /* KONTAKTVERZEICHNIS_H_ */
and this is kontaktfunktionen.c
#include <stdio.h>
kontakt[];
kontakt_hinzufuegen(){
int i = 0;
printf("Bisher sind %i Kontakte angelegt.",kontakt[i]);
kontakt[i++];
}
struct kontaktname{
char* name;
char* vorname;
};
struct kontaktanschrift{
char* strasse;
int hausnummer;
int plz;
char* ort;
char* land;
};
Where is my error?
You're not supposed to #include C files, that's not the proper way to organize your code.
You should compile the C files separately and then link them together, or compile them all at once with a single compiler invocation.
Do not #include anything in your header file. And do a #include "kontaktverzeichnis.h" in the kontaktfunktionen.c file.
As #StoryTeller commented, define your kontakt_hinzufuegen() as int kontakt_hinzufuegen() in the kontaktfunktionen.c file and return an int value from the function kontakt_hinzufuegen as for ex::
#include <stdio.h>
#include "kontaktverzeichnis.h"
// define the type for this array as below
int kontakt[];
int kontakt_hinzufuegen(){
int i = 0;
printf("Bisher sind %i Kontakte angelegt.",kontakt[i]);
kontakt[i++];
// Return an int value
return 0 ;
}
Your error is that in kontaktfunktionen.h you are including kontaktfunktionen.c. This will include all the definitions and declarations from kontaktfunktionen.c which are already declared when you use kontaktfunktionen.c
As others have said: You should not include .c files in your header files.

Resources