Sign in to follow this  
x22

C++ Class Declerations

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
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

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