Declared variable in header file causes a C4430 error

closed account (jNUpSL3A)
I have a header file called "Engine.h", and a cpp file "Engine.cpp", i declare a variable that is type of a struct/class (doesn't matter at the moment), then i use #include "Engine.h" in the "Engine.cpp" file, and no matter what i try in the "Engine.h" file, putting "static", "extern", etc.. on the declared variable, it still gives the same error message:
Error C4430 missing type specifier - int assumed. Note: C++ does not support default-int (points at line 1 in Engine.h file).

Engine.h
1
2
3
4
5
6
7
static TIME Time;
struct TIME
{
	double deltaTime = 0;
	double fixedDeltaTime = 0;
	double unscaledDeltaTime = 0;
};


Engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "Engine.h"
using namespace std;
int main ()
{
	bool isRunning = true;
	while(isRunning)
	{
		cout << Time.deltaTime << endl;
	}
	return 0;
}
You have to declare struct TIME before you can define a variable of that type.

It's almost always a bad idea to define a variable inside a header file. It's okay to to declare it (e.g., extern int xyz), but you usually don't want to define it. (e.g. int xyz). Defining it allocates space for the variable and if two .cpp files include the same .h file, they will both try to allocate space for it and the linker will complain.

It's also generally a good idea to define your variables in the narrowest scope necessary. So in this case, given the code you've show, Time should be a local variable inside main().
Maybe this:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Engine.h
struct TIME 
{
    ...
};

extern TIME Time;


// Engine.cpp
#include "Engine.h"

TIME Time;


Or if you only need to access Time in Engine.cpp, then get rid of the "extern TIME Time" statement in Engine.h.
closed account (jNUpSL3A)
Thanks for your reply, yeah i know it is a good idea to define it in main(), but what i really want is to organize the code and make it as clean as possible, so it'll be easier to modify when i need to change something, i moved the struct upwards and now it works fine.

But i have to ask, why defining it is bad? can't the header file have 1 global variable that can be accessed/modified anywhere by using #include "Engine.h" ?
closed account (jNUpSL3A)
What i want to do is make a global variable that could be used anywhere when needed, for example if i have a movement cpp file, i would like to do something like:
#include "Engine.h" or using namespace Engine; .
The second code won't probably work because it doesn't have a reference to the Engine.h file, so it leaves me with the #include one.

Maybe i should define the "Time" variable when needed in another file? like the movement example?

Edit: the Time.deltaTime variable has to be modified in the Engine.cpp file, and once it gets modified, it should be accessed with that value, for example in the 1st frame, if i set it to 0.01, the other cpp files should receive 0.01 too when they read from the Time.deltaTime variable.
Last edited on
closed account (jNUpSL3A)
Fixed it, both of your solutions work, thanks.
Topic archived. No new replies allowed.