• Advertisement
Sign in to follow this  

C++ Class Declerations

This topic is 3674 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings, greetings, I'm using Code::Blocks to create a project, and I'm confused about the concept of class declerations, as C::Bs outputs, what seems to be, contradicting errors. Now, I have two classes. We'll call them Logger, and Window for simplicity.
// utility.h ---------
#include <mylogger.h>
#include <mywindow.h>
// main.cpp ----------
#include <utility.h>
// mylogger.h --------
#include <utility.h>

class Logger
{
    ...
};
// mywindow.h --------
#include <utility.h>

class Window
{
    public:
        myfunc( ..., Logger * );
}
Keep in mind, this is really trimmed down, and has header guard words etc., etc., but I think it gets my point across. Now at mywindow.h, in myfunc() C::Bs says that Logger has not been declared - however - changing mywindow.h to the following says that Logger is already declared, and I can't declare it again.
// mywindow.h ---------
#include <utility.h>

class Logger { };

class Window
{
    public:
        myfunc( ..., Logger * );
}
I'm curious what I'm doing wrong, how to set the Logger class as a parameter, and is there a better way to tell the Window class to use Logger? -- Aaron

Share this post


Link to post
Share on other sites
Advertisement
To do a forward declaration (what you're trying to do in the second part of your post), you don't need (or want) the {}'s. Just plain ol':

class Logger;

And don't forget to include mylogger.h in mywindow.cpp!

Share this post


Link to post
Share on other sites
Thanks, works like it should now, but I can't shake the feeling that I might be dumbing down my system by having to declare the class fowardly. I would think that since both files include the utility, which includes the Logger, then the Window - that Logger would already be declared and ready for use. Aparently thats not so though.

-- Aaron

Share this post


Link to post
Share on other sites
You may be including <mywindow.h> somewhere which caused your error. In visual studio ther is an option to tell the compiler to ouput explicitly the included files for each cpp. I don't know about Cb but if such an option exists it might help you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement