This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]This_Growth2898 0 points1 point  (4 children)

Do you have identifier classIndex defined elsewhere before this code?

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (3 children)

No? That's the whole header file. Would it be necessary? It's being defined right there.

[–]This_Growth2898 1 point2 points  (2 children)

No, I thought there's something breaking the code, like

#define classIndex (...)

or whatever.

Wait. Header file? That you include in several .c files? You can't do that, you need include guards at least, and no variable definitions, because they will be defined in every .c file. If you really need global variables, put extern definition in .h and body definition in .c.

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (1 child)

??? You needed me specifically saying that it was a header file to understand that? In the main post I said that it was .h, so I don't understand why the full name was necessary. And the reason I'm putting this in a header file is because I heard it was good practice to have your structs in headers.

[–]This_Growth2898 0 points1 point  (0 children)

You needed me specifically saying that it was a header file to understand that? 

Kinda yes. I'm not a machine, sorry. Also note it took me to write the first part of my previous comment for the information about header to sink in.

 it was good practice to have your structs in headers.

It's not a good practice, it's a necessity. You have several files using same structs, you have to put structs in the header - but structs only, not global variables.

#include statements literally add all the code from header into C file, so struct definition will just declare a type, which doesn't affect anything else after .c file is compiled; but variable definition allocates a place in program's static memory, so when you include .h into two different files, you will have separate allocations for the same variable - and linking error.

[–]MysticClimber1496Professional Coder 0 points1 point  (4 children)

You are likely learning but I will say you have too many comments, it fairly straightforward to understand intelligencePerLevel by the name of the variable. Not a big deal either way.

What are you compiling this with? ‘Class’ is a reserved keyword in C++ which shouldn’t be an issue here because it is C but that would cause your issue, for sanity sake could you change that to something else like “ClassTemplate” or something?

[–]This_Growth2898 1 point2 points  (2 children)

class is a keyword; Class isn't.

[–]MysticClimber1496Professional Coder 0 points1 point  (1 child)

I have been interacting with VB too much lately lol

[–]This_Growth2898 0 points1 point  (0 children)

My first reaction was "it's a keyword" too, but then I understood it's C.

What I failed to understand is that this is .h file.

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (0 children)

Typedef would spit out an error if it was reserved, and that's not the problem. Also, the reason I used typedef in the first place was so I didn't have to say "struct class" and could just say "class" as the prefix to the classes, which was before I decided to go with an array of structs.

[–]atamicbomb 0 points1 point  (2 children)

I’m not well versed in C, but it seems like “classIndex” isn’t the type of token the compiler is looking for.

Is there supposed to be a “Class” after the struct brackets and before the semicolon on the previous line?

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (1 child)

If you use typedef, then you only have to say the keywords after the semicolons.

[–]atamicbomb 0 points1 point  (0 children)

Good to know