Sign in to follow this  
adol

illogical compiler errors

Recommended Posts

Note: Using Visual Studio 6 When I include a class I wrote and try to implement it, the compiler generates errors C2146(missing identifier) and C2501(missing storage class). However, when I right click on the type and click, goto definition, the compiler finds it without trouble. In general I'm having trouble with the compiler locating definitions when they are blatently obvious. Any help/advice?

Share this post


Link to post
Share on other sites
Quote:
Original post by adol
Note: Using Visual Studio 6

When I include a class I wrote and try to implement it, the compiler generates errors C2146(missing identifier) and C2501(missing storage class). However, when I right click on the type and click, goto definition, the compiler finds it without trouble. In general I'm having trouble with the compiler locating definitions when they are blatently obvious. Any help/advice?


The problem is when you right click on the type the 'IDE' finds it. Im not sure how the parsers for the IDE and compiler is connected but they may not nesseraly function the same and the compiler, when compiling, is actualy building the object files so i find it more strict. If i was to guess the IDE works out what type is what but using a diffrent system to the compiler.
(Just a guess though)

Share this post


Link to post
Share on other sites
This nasty little problem also crops up if there are circles in your includes.

a includes a,b
b includes a,b

then b tends not to find what it needs.

a inc a,b
b inc b,a

may find what it needs because the include order has changed.

took me days to track this down in one of my programs because the loop covered 20 or so source files.

Share this post


Link to post
Share on other sites
Kul Seran, your hypothesis of the problem is accurate, my program seems to be very circular inclusive. However, I tried to remedy the problem with the #ifndef/#define. How do I overcome this issue? Reorder the inclusion of the header files?

Share this post


Link to post
Share on other sites
If you class A only uses class B as paramteters in the member methods of clas A then you can forward declare class b like so: class B; at the top of your header file. You will need to include class B's header file in A's implementation file or you will get an error.

This method won't work if you have actual objects of B but this can be resolved by using pointers instead and allocating your B objects on the heap via new().

Share this post


Link to post
Share on other sites
If a class doesn't contain an actual object of another class it doesn't need its full definition, it can cope with knowing only the name.


//No Problem
class SomeOtherClass;//only need name
class SomeClass {
SomeOtherClass* m_pSoc;
SomeOtherClass& m_refSoc;
void SomeFunction(SomeOtherClass);
};


//Problems
class SomeOtherClass;//only have name
class SomeClass : public SomeOtherClass {//can't inherit without full definition
SomeOtherClass m_soc;//can't have member as need to know size of SomeOtherClass
void SomeFunction(SomeOtherClass soc) {
soc.hello();//can't use SomeOtherClass object
}
};


Once you start making use of SomeOtherClass, i.e. calling functions, making use of its size etc, you'll have to have the full defintion (i.e. the header "SomeOtherClass.h").

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this