User forums > Using Code::Blocks

Strange error when using a custom struct



--- Code: ---
struct GLF_Header {
    unsigned short int charCount;
    unsigned long int ColorKey;

struct GLFEntry {
    unsigned char charCode;
    unsigned long int xPos;
    unsigned long int yPos;
    unsigned long int Width;
    unsigned long int Height;

GLF_Header GLFH;
GLF_Entry GLFE[256];

--- End code ---

Well thats my code, and these are my errors

--- Quote ---
GFX_Routines.c:20: parse error before "GLFH"
GFX_Routines.c:20: warning: data definition has no type or storage class
GFX_Routines.c:21: parse error before "GLFE"
GFX_Routines.c:21: warning: data definition has no type or storage class

--- End quote ---

Now my problem is, is to me, this code all looks completely ok, but for some reason, I get these errors, any help would be great.

Could it be that the "unsigned short int" and such aren't recognised properly? You could try your code with just "unsigned short" and see what happens.

First, you have a typo (maybe a copy-paste error, but nonetheless):

--- Code: ---struct GLFEntry { .. }
GLF_Entry GLFE[256];
--- End code ---

My guess is that this file has a .c extension, which makes the compiler treat it as a C file, not C++. This, in turn, means that you must program "the C way", i.e.:

--- Code: ---struct GLF_Header { ... }
struct GLF_Entry { ... }

struct GLF_Header GLFH; // <-- notice the 'struct' keyword
struct GLF_Entry GLFE[256]; // <-- here too
--- End code ---

or if you don't want to use the 'struct' keyword:

--- Code: ---struct GLF_Header { ... }
typedef struct GLF_Header GLF_Header; // <-- 'typedef' it

struct GLF_Entry { ... }
typedef struct GLF_Entry GLF_Entry;  // <-- 'typedef' it

// since the structs are "typedef'd" now, you 're OK with this
GLF_Header GLFH;
GLF_Entry GLFE[256];
--- End code ---

Your other option, is to rename the file to .cpp so the compiler treats it as a C++ file...



[0] Message Index

Go to full version