forward class declaration not working

Started by
8 comments, last by M2tM 13 years, 5 months ago
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]
YES I use gamemaker, YES I'm proud of it, NO I don't want to move on yet..WHY? because I don't want to program the interface etc, I just want to focus on simulations with physical formulas and AI pathfinding codes..
Advertisement
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

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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 {};

YES I use gamemaker, YES I'm proud of it, NO I don't want to move on yet..WHY? because I don't want to program the interface etc, I just want to focus on simulations with physical formulas and AI pathfinding codes..
You can't have a member of type Origin before the definition of Origin. The forward declaration would allow you to have a pointer or a reference to Origin, but not an Origin itself.
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?
YES I use gamemaker, YES I'm proud of it, NO I don't want to move on yet..WHY? because I don't want to program the interface etc, I just want to focus on simulations with physical formulas and AI pathfinding codes..
What exactly do you want to do? Initially you said that Mask's constructor takes an Origin, but your latest code has it as a member...
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.
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
YES I use gamemaker, YES I'm proud of it, NO I don't want to move on yet..WHY? because I don't want to program the interface etc, I just want to focus on simulations with physical formulas and AI pathfinding codes..
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.
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.};
_______________________"You're using a screwdriver to nail some glue to a ming vase. " -ToohrVyk

This topic is closed to new replies.

Advertisement