Archived

This topic is now archived and is closed to further replies.

leggyguy

a frustrating problem

Recommended Posts

I have a module which is bothering me. I use in it a class called Game, which is my main class for the program. Now, this particular module doesn''t seem to want to accept that this "Game" class type exists. Everywhere where I use an instance of the class in this module, I get errors along the line of: "left of ''->DeliverPass'' must point to class/struct/union" which I would expect to receive if I haven''t included the classes .h file. However, if I add the classes .h file, I get errors in other modules that were working perfectly fine beforehand. To solve this I have announced that yes we will be using the Game class by declaring its existence with "class Game;", which has worked before, but not this time. I feel I am doing something wrong with my #includes, because the code itself is fine, and works when used in other programs. But this time when I include a file in one .h file, another file starts spewing errors associated with missing class types (because of the mess with the header includes) Is there perhaps an article I can read online which can help me clear up my use of includes? Anyone know?

Share this post


Link to post
Share on other sites
I believe that your problem is how you are dereferencing your objects. I think that that error comes about when you use the "->" symbol when you should be using the "." symbol. If you have declared a pointer to a game object, then newed a game object you would use the "->" dereference to access the member methods and data but if you declared a Game object in the main program you would use the "." dereference.

Pointer Method:
  
int Data;
Game *ptrToGameObject = new Game();
ptrToGameObject->ClassMethod1();
Data = ptrToGameObject->ClassData;


Standard Method:
  
int Data;
Game GameObject();
GameObject.ClassMethod1();
Data = GameObject.Data;


Cheers,

RandomTask

Share this post


Link to post
Share on other sites
Yeah, it sounds like your problem *Might* be with the setup of your files. Not sure. Anyway, if you are using Microsoft Visual C++, you might want to go to TOOLS, OPTIONS, and then the Directories Tab. Here, put the path of your Game class( .CPP and .H files), along with your other class and header files in here. The Visual C++ compiler will always search these directories. Not sure exactly what your problem is. You will want to review the first post. It could have to do with your dereferensing.

Edem Attiogbe

Share this post


Link to post
Share on other sites
quote:
Original post by leggyguy
I have a module which is bothering me. I use in it a class called Game, which is my main class for the program.

Now, this particular module doesn''t seem to want to accept that this "Game" class type exists.

Everywhere where I use an instance of the class in this module, I get errors along the line of:

"left of ''->DeliverPass'' must point to class/struct/union"

which I would expect to receive if I haven''t included the classes .h file. However, if I add the classes .h file, I get errors in other modules that were working perfectly fine beforehand.

This means your header file probably either
a) needs inclusion guards if it is an error at compile time, or
b) needs you to remove functions or variables from it if the error is at link time.

quote:
To solve this I have announced that yes we will be using the Game class by declaring its existence with "class Game;", which has worked before, but not this time.

It will only work if you are just passing around pointers or whatever. If you want to use the contents of ''Game'', which you are doing as soon as you use the ''->'' operator upon it, you must define it fully by including the header.

quote:
Is there perhaps an article I can read online which can help me clear up my use of includes? Anyone know?

Probably. But before someone else posts you a link, here''s the basics:

Don''t declare function bodies in headers: only function prototypes;
Don''t declare variables there: only extern references to them.
Do declare constants (whether #defines or const variables)
Do declare inline functions
Do declare templates (classes and functions)
Always use an inclusion guard

If you don''t know what an inclusion guard looks like, it''s like this:

(start of header file)
#ifndef MY_HEADER_FILE_H
#define MY_HEADER_FILE_H

// all the header here...

#endif // MY_HEADER_FILE_H
(end of header file)

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Thanks for the replys.

My problem arises from this:

I had a fairly large program for me, consisting of about 14 modules. It was working perfectly, but I wanted to encapsulate it in a tidier way.

I did this, and it was still working fine. But I noticed some overlapping of #inlcude files, so decided to clear that up. To that end, deleted all my #include directions, and decided to put them all in a better way. The referencing to pointers and actual objects is fine, as the program was working fine before removing the #include lines.

I have now finished including headers, and I am getting a hundred errors telling me that "BG uses undefined class ''BackGround''" and similar errors for many of the instances of my classes. I am getting this error, despite having included the BackGround.h file and having announced in the code straight after the include "class BackGround;".

I do use guards, as someone mentioned before, so that should not be a problem. I am really puzzled, because the code was working well before I removed my #include lines, and now that I have rearranged them it isn''t, even though I am #inlcuding the files that are required.

Share this post


Link to post
Share on other sites
Does it matter what order you include headers?

I always worked on the assumption that is a file was #included, then that ws fine. My books never mentioned you had to inlcude files in any particular order.

Share this post


Link to post
Share on other sites
Absolutely positively. Maybe it will help if you understand what #include actually does:

You can think of #include as a macro (well, it actually is). What #include does is tell the compiler to physically include the entire file at that point in the .cpp file. So when your .cpp file has an #include line, you physically have that text in your file at that point.

The style I''ve picked up (thanks to "Large Scale C++ Design" by Lakos) is that each module in my project is split up into two files--one .cpp file and one .h file named after the module--and the very, very, very first line in the .cpp file is to #include the module''s .h file. This lets you track down bizarre include dependencies.

The exception to this rule is when you use precompiled headers; then, the PCH must be the first #include.

Share this post


Link to post
Share on other sites
quote:
Original post by leggyguy
I am really puzzled, because the code was working well before I removed my #include lines, and now that I have rearranged them it isn''t, even though I am #inlcuding the files that are required.

It''s simple: if you remove a #include "myclass.h" from somefile.cpp, and myclass.h is the file that contains the definition of ''MyClass'', then you can''t use MyClass''s members or functions in somefile.cpp. Simple as that. Any file that needs access to the details of a class will need to include the header that defines that class, either directly or indirectly. Rearranging stuff won''t make any difference unless you are including everything that you need to.

Share this post


Link to post
Share on other sites