Compile errors with generated.h

I’m getting the compile error:

Expected an include at the top of the header: ‘include “MessageHandling.generated.h”’

in my header file “MessageHandling.h”. The error seems to be caused by a macro in the file which includes the UCLASS macro:

#define DECLARE_MSG_TYPE_AND_DATA(messageName, messageDataClass) \
	UCLASS() \
	class messageName : public UObject, public messageDataClass { \
		GENERATED_BODY() \
		public: \
			template<typename... Arguments> \
			messageName(Arguments&&... args) : messageDataClass(std::forward<Arguments>(args)...)			{ } \
	};

If I include MessageHandling.generated.h at the top of the file I instead get the error:

Cannot open include file: ‘MessageHandling.generated.h’: No such file or directory
So I’m not sure how to make the system happy. If I remove the UCLASS macro from this macro then the error goes away, but we need it, so, yeah… The generated.h files have been a constant source of headache for our project, so perhaps someone can direct me to some documentation about how the system works so we can finally get some work done.

How do you add code to your project? The .generated.h files are, like their name implies, automatically generated when you add a class to your project. Maybe you are adding the class without using the “Add Code to Project” in the editor?

This macro won’t work because UHT cannot expand it. The same with conditional compilation (#if, #else, …). You have to define each class header by hand instead of doing it with handy macro.

I was afraid of that. I already ran into the issues with #if/#else, and had a feeling it was related. It’s a bummer to see the limitations imposed by their use of an independent header/reflection tool. It’s been a constant pain for us with its restrictions on how we structure our code.

If you regenerate your solution, it will create the .generated.h files.

See this answer for more info on generated files.

This answer describes how to force regeneration.