Sign in to follow this  

forward class declaration not working

This topic is 2590 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

Hi everyone,

I'm programming a bit again. However I'm running into problems with the order in which classes are declared.

I have 1 class which is called "Mask" and another "Origin". These cause some problems, the "Mask" constructor takes an origin which causes the following compile error:

Quote:
error C2079: 'Mask::orig' uses undefined class 'Origin'


My headers look like:
mask.h
#include <vector>
class Mask {...};

generic.h
class Mask;
class Origin;
#include "mask.h"
class Origin{...};




I know that this bug happens because "Mask" is created before "Origin" is. However can I prevent this bug from happening? (Other than by making sure Origin's code is above Mask's?) - I tried to do this by putting the line "class Origin;" before the include, but that didn't help?

[Edited by - paul23 on November 10, 2010 1:37:59 PM]

Share this post


Link to post
Share on other sites
Have you tried declaring "class Origin;" in mask.h? EDIT: or #include "origin.h"?

Is there a reason you include mask.h in origin.h? If you don't need Mask for the Origin class:

mask.h
======
#include "origin.h"
class Mask {...};

NOTE: each of your header files should have at the top:

#pragma once

to prevent circular includes.

EDIT: an alternative to #pragma once -

mask.h
======
#ifndef _MASK_H
#define _MASK_H

#include <vector>
// etc.

// last line
#endif

Add similar preprocessor directives to each header file.

#ifndef _ORIGIN_H
#define _ORIGIN_H
...
#endif

Share this post


Link to post
Share on other sites
Well indeed I use the #pragma once prepocesor directive..

I tried to place the forward declaration inside the mask-header, and to include the generic header.. Both yield the same problem..

Heck even if I place everything inside 1 header, but place the code for the origin after the code for the mask it still won't work

Below is all the code I reduced my program too which still gives the same error:


class Origin; //this line is ignored???

class Mask {
public:
private:
Origin o;
};
class Origin {
};

Share this post


Link to post
Share on other sites
ow ok.. So what you are basically saying: "there's no way around this"?


Then how is the best way to organize this? - Adding an extra header file which calls all other headers in a specific order?

Share this post


Link to post
Share on other sites
Quote:

Then how is the best way to organize this?

Is there a circular depedency? If not, just have mask.h include origin.h and be done with it. If there is, you'll have to break the cycle by using forward declarations and pointers or references anyway.

Quote:

Adding an extra header file which calls all other headers in a specific order?

No, certainly not. That kind of approach causes these sort of problems, and can cause compile times to increase aswell.

Share this post


Link to post
Share on other sites
Well the problem is morely that

"generic.h" is a header which I used for all those small global functions & classes. (like origin, which is nothing else than an x,y value) Some which DO depend on masks however.

So I really have to rework the header file :P

Share this post


Link to post
Share on other sites
Or just remove it. Seriously, such headers are more trouble than they are worth.

You can get away with a per-module header, provided it is not referenced inside the module. For instance, a class in the game module could include graphics.h and get origin.h and mask.h included for it, but no classes inside the graphics module would reference graphics.h.

Share this post


Link to post
Share on other sites
There's a difference between referencing an item via pointer before it is defined and making an instance of it.

This is illegal because the compiler needs to know how big the objects it contains are:


class B;
class A{
B var; //How big is B? We don't know yet, it hasn't been defined, only declared.
};

class B{
A var; //We don't even get to this point, but it would be ok if A didn't have a problem in it.
};




This is ok.

class B;
class A{
B *var; //This will work, we are storing a pointer to a B variable, we know how big a pointer is.
B &var; //Same deal.
};

class B{
A var; //this is ok, A has been defined.
};


Share this post


Link to post
Share on other sites

This topic is 2590 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.

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