#include Nightmare

Hi,

I have a quite strange and fundamental problem. I have encounered this for several times, so far. But every time it happened, I edited the code to get rid of the problem some how, to the prejudice of the original strucutre of code.

Now I'd like to beat this problem.

Well. I have a project which is not the smallest one. There are some .h and .cpp files (surprise).

I decide to add some methods to some class. Thats why I had to add some includes. After adding the includes, the project cannot be compiled.

The compiler sayes: syntax error : missing ';' before '*' or similar statements. On my opinion, it is a implication of some problem in the includes. This is, similar compiler errors occure, when the return type of a method or type of an data member is not declared. BUT it certainly IS (in the include)!

I think, there is some problem in the logic of nested includes. I mean: #include directives can be found both in .h and .cpp files. One file includes the second, which includes the first and so on, and finaly some data or functions is not included at all. I am surely making scifi... but I really have no clue, where is the problem.

A one means of solving this is to make a class declaration of style:
class MyClass;
which sometimes solves the problem for me. This declaration of class I place in stead of an #include directive, and it really functions, some times, but I dont know why.

I am not sure if posting some code would be useful, because I really dont know, where the problem is... :(

Please, somebody anybody, HELP me! I cant sleep well until I find a solution.

Regards,
MartinCz.
[1732 byte] By [MartinCz] at [2007-11-19 6:12:22]
# 1 Re: #include Nightmare
I think, there is some problem in the logic of nested includes. I mean: #include directives can be found both in .h and .cpp files. One file includes the second, which includes the first and so on, and finaly some data or functions is not included at all. I am surely making scifi... but I really have no clue, where is the problem.

Circular dependecies usually generate LNK2005 errors not syntax errors. First thing you need to do is remve all unnecessary #include statements. I prefer to keep as many of my #include staments in my .h files as possible because it reduces the likelyhood of circular dependencies (at least it seems to for me).

The compiler sayes: syntax error : missing ';' before '*' or similar statements. On my opinion, it is a implication of some problem in the includes. This is, similar compiler errors occure, when the return type of a method or type of an data member is not declared. BUT it certainly IS (in the include)!

I have never seen a syntax error generated when there wasn't an actual syntax error. It's possible I suppose but not too likely.

Without code to look at though, this is all speculation/assumption. And we all know how dangerous assumptions can be...
mlgoff at 2007-11-11 0:32:04 >
# 2 Re: #include Nightmare
This is what it says:

error C2143: syntax error : missing ';' before '*'
error C2501: 'CThumbnailsView' : missing storage-class or type specifiers
error C2501: 'GetThumbnailsView' : missing storage-class or type specifiers

This is what it complains about a function returning CThumbnailsView* for exapmle. I have learned the comiler says this when he cant find that type. But I have that in a .h file FOR SURE!
MartinCz at 2007-11-11 0:33:07 >
# 3 Re: #include Nightmare
I prefer to keep as many of my #include staments in my .h files as possible because it reduces the likelyhood of circular dependencies (at least it seems to for me).You probably wanted to say: "keep as many of my #include staments out of my .h files". That's the way it should be.

For resolving the #include nightmare, See my comment in this thread (http://www.dev-archive.com/forum/showthread.php?p=1101533#post1101533).
gstercken at 2007-11-11 0:34:09 >
# 4 Re: #include Nightmare
I have learned the comiler says this when he cant find that type. But I have that in a .h file FOR SURE!That might be - but circular #includes, along with include guards, can cause a header file which is actually #included not to be looked at nonetheless.
See the link in my previous post.
gstercken at 2007-11-11 0:35:14 >
# 5 Re: #include Nightmare
Oh My God! Hope Your Link Will Help!!! :d
MartinCz at 2007-11-11 0:36:15 >
# 6 Re: #include Nightmare
I think, there is some problem in the logic of nested includes. I mean: #include directives can be found both in .h and .cpp files. You are right.
Nested #include can create havoc and become a nightmare.

As a rule I never use #include in a header file with an exception of some class declarations that are not heavily dependent on other.
For example if I subclass control using MFC class I usually put this class header in dialog header file; that does not create ripple effect. Another story is when variables of other types are added to those classes; than I definitely remove #include from dialogs header.

Whenever pointer of some type is declared put include in a cpp file (order is irrelevant) and use forward declaration in a class header.

To cure your problem (it may take some time):
First remove all (except above mentioned) includes from headers.

You will see compiler errors complaining about unknown types in some header files.

Look at output and see what cpp is compiled and what header compiler is complaining about.
Add #include in that cpp before header compiler found problematic.
Continue doing it until you clear all problems with no (or minimal) #include in header files.

You may end up with less #include this way than if using them in a headers not to mention circular dependencies.
JohnCz at 2007-11-11 0:37:14 >
# 7 Re: #include Nightmare
Thank you friends!!! I will do what you have mentioned and HOPE it will help. I will post how the result of my effort. :D

I did not know it is a problem to make an #include directive in my .h files. I only had a really large feeling. :D And - as I see - it is true;

Well, once again,

I really appreciate your fast and full-descriptive answers.

Thank you all!!!

Regards,
MartinCz.
MartinCz at 2007-11-11 0:38:17 >
# 8 Re: #include Nightmare
Yes yes yes yes yes.

Nightmare is beaten!!!!

I am happy as at Christmas.

Thank you. After a long while of psychic depression, I could see 0 errors and 0 warnings. Yes yes yes.
MartinCz at 2007-11-11 0:39:10 >
# 9 Re: #include Nightmare
It took you 15 min (at least between posts.) it was not that bad.
I had problems that I had to fix taking more than several hours of painstaking, tedious re-including.
JohnCz at 2007-11-11 0:40:19 >
# 10 Re: #include Nightmare
Funny is that I compiled it after correction ok. Then I cleaned the bin files and when wanted to compile again ... woops 432 errors. :D

I had to do some changes ... I had to put includes to the .cpp files where there where missing after removing them from .h files.

Now, it is ok, which is so groooovy!! :D :wave:
MartinCz at 2007-11-11 0:41:20 >