Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Header Guards Causing Classes to not be Recognized
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HRI




PostPosted: Fri Apr 15, 2011 9:05 am   Post subject: Header Guards Causing Classes to not be Recognized

I've been working on a project with multiple classes. Now when I try to use the classes in other file (eg for a parameter of another class's function), it comes up with the error:
c++:
error: <classname> has not been declared

At the start this only applies to one class.
If I remove the header guard of this class
c++:
#ifndef _<classname>_H_
#define _<classname>_H_
...
#endif

The other class header files included for this class start to have the same problem. If I remove the header guard for one of the other classes that includes this one, the compiler goes into a compiling loop of repeated including.

The odd thing is that I've used header guards before and had them work perfectly fine, so I'm not sure why this isn't. The only thing new is that I'm using many classes (just to see how it works out).


Before this problem I had a namespace with all of my objects needed for this project inside it, but these objects were not retaining their data members' values from function to function so I decided to just throw in a bunch of parameters and arguments instead. The code did compile and run with the namespace, as well as the guards, but now when I try to have parameters of other classes it won't.

This was all done with the GNU compiler (the default for C::B).

I see this as a pretty general problem, but let me know if you need more specific context.
Sponsor
Sponsor
Sponsor
sponsor
apython1992




PostPosted: Fri Apr 15, 2011 9:54 am   Post subject: RE:Header Guards Causing Classes to not be Recognized

Header guards are likely not your problem, and should always be used. Remember that for you own classes, the include statement should have quotes, not <>.

c++:

#include <iostream>      // From standard library

#include "someclass.h"   // Your own class
apython1992




PostPosted: Fri Apr 15, 2011 9:57 am   Post subject: RE:Header Guards Causing Classes to not be Recognized

Though you seem to have done this kind of thing before, so on second thought you probably already know that...are you sure you don't have any name collisions? Having two classes of the same name would be a likely cause.
apython1992




PostPosted: Fri Apr 15, 2011 10:00 am   Post subject: RE:Header Guards Causing Classes to not be Recognized

And by likely I mean most certainly.
HRI




PostPosted: Fri Apr 15, 2011 10:28 am   Post subject: Re: Header Guards Causing Classes to not be Recognized

I am absolutely sure that the "" are being used and that there are no name collisions. I changed one of my class's names from Player to Person and it still did the exact same thing. Also, when the guard is removed from that "Person" header it happens to every other class included so all of those names can't conflict.
apython1992




PostPosted: Fri Apr 15, 2011 2:41 pm   Post subject: RE:Header Guards Causing Classes to not be Recognized

Interesting. Would you be willing to upload your project so I (and potentially more helpful others) could take a look?
HRI




PostPosted: Mon Apr 18, 2011 8:45 am   Post subject: Re: Header Guards Causing Classes to not be Recognized

Sorry, haven't even been able to think over the weekend let alone go on the computer. Keep in mind I'm trying out new things and some (if not most) of these will be inefficient, not to mention it's a first-year C++ class.

By the way, it's supposed to be a connect-four AI, though the checkwins and AI functions haven't really been written.

The files are here.
Dratino




PostPosted: Mon Apr 18, 2011 11:02 am   Post subject: RE:Header Guards Causing Classes to not be Recognized

Perhaps there are two files that are trying to include each other. That caused problems for me before.

In cases like that, you should forward-declare the class in the headers (without using the include), because they'll be defined elsewhere and you most probably won't be using them in the headers anyway.

So, if you have a file "Player.h" that depends on a class Ball, declare the Ball class like this:

code:
class Ball;


And just leave it at that. This will work as long as you're not accessing any of Ball's functions from Player.h, and just containing pointers to the Ball class.
Sponsor
Sponsor
Sponsor
sponsor
HRI




PostPosted: Mon Apr 18, 2011 1:05 pm   Post subject: Re: Header Guards Causing Classes to not be Recognized

Well, that cleared up the main problem I was having (and yes, I know I still need to get around to rearranging the board array), but now it gives
code:
"error: invalid use of incomplete type 'struct Input'

pertaining to the line in a function of the Game class that uses the Input & object, among other places.

Also,
code:
error: forward declaration of 'struct Input'

on the lines of, for example, "class Input;".

-------------------------------------------------------------------------------------------------------------



Actually, I looked back and removed one of those class definitions where the one that I was creating didn't include the header file it was in (if that makes sense), and now all of those errors seem to be gone! I just need to make the 2d board array so I can properly pass it to everything.
HRI




PostPosted: Mon Apr 18, 2011 1:34 pm   Post subject: Re: Header Guards Causing Classes to not be Recognized

All right! Everything seems to be working, minus a small error with the value returned for which player goes first not being one of the two returns...
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 10 Posts ]
Jump to:   


Style:  
Search: