Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ compilers bitch when I do this

Name: Penis Moai 2006-08-12 16:58

Say I have two .cpp files and their corresponding .h files.  For the sake of brevity I'll leave it to just the parts that cause trouble:

==============================

//-------
//actor.h
//-------

#include "mapdef.h"

class Actor {
 ...
 private:
    Level *parent_level;
 ...
};
==========================

// ----------
// mapdef.h
// ----------
#include "actor.h"

class Level {
  ...etc etc...
 
  private:
     Actor *actors;   // I'd use a linked list, vector, or something more flexible in the actual code
...etc etc blah blah blah...
};

==========================================================

I also use #ifdef _ACTOR_H blah blah blah to make sure header files are never loaded more than once in the preprocessor (which causes compilation to terminate w/ error), like good programming practice.

Anyway, because of the way the preprocessor does things, this causes problems.  Say a .cpp file does an #include "mapdef.h".  The Level class refers to an Actor, and the mapdef.h itself #includes actor.h so it can "know" what an Actor is.

...but the compiler gets confused because then it looks in Actor class and it refers to a Level object... but Level isn't finished being defined yet.  Because these two classes refer to each other in some way for necessary objects/pointers/etc, the compiler shits.

I do find that adding "class" before objects fixes it:

class Actor {
  (blah blah blah)
  private:
     class Level *parent_level;
  (blah blah blah)
};

class Level {
  (blah blah blah)
  private:
     class Actor actors[200];
  (blah blah blah)
};

But I've never seen this done before in anyone else's code, and I'm not sure this is "kosher" by ANSI/ISO standards even though gcc and Visual C++ allow it.

The way I fix it... am I doing it right?  Or is there a better, less iffy way?

Name: Anonymous 2006-08-12 17:02

You can do it the way you did, or you could put some class Actor; and class Level; before the definition of the opposite classes. (I think I got the syntax right)

Name: Anonymous 2006-08-13 11:58

>>1
What you are doing is a forward definition of a class.  You would normally do it by putting something like "class Actor;" around the top of the file like >>2 said all awkwardly.  However, if you are using two classes in each other, you really need to analyze the structure of your program and make sure that it is really necessary because this is bad OOP.

Name: Anonymous 2006-08-14 4:05

Yeah, it's not best practices if you make anything useful with your classes.

Name: Anonymous 2006-08-15 12:14 (sage)

>>3
because enterprise scalable solutions require 100 classes in each other, amirite?

Name: Anonymous 2009-01-14 14:09

YOU ARE NOW THINKING MANUALLY

Name: Anonymous 2009-03-06 13:20

right its just not going to give   your program a   BETTER ONE LOLOLOLOLOL   Quite true This   is at a   cat riding a   bus and suddenly   a black snake   head with the   most retarded of   questions a recommendation   of withdrawal note   the rest of   them I do   real work in   a world like.

Name: Anonymous 2010-06-28 10:58

beware the army of 12 year old autistics

Name: Anonymous 2011-02-04 17:18

Name: Anonymous 2011-06-16 2:08

[over][under]EXPERT BBCODE PROGRAMMER[/under][/over]

Name: Anonymous 2011-06-16 2:09

EXPERT BBCODE PROGRAMMER

Name: Anonymous 2011-06-16 2:09

EXPERT BBCODE PROGRAMMER

Name: Anonymous 2011-06-16 2:09

EXPERT BBCODE PROGRAMMER

Name: Anonymous 2011-06-16 2:10

EXPERT BBCODE PROGRAMMER|||[overline]test[/overline]

Name: Anonymous 2011-06-16 2:11

[b]_______         

Name: Anonymous 2011-11-27 2:48

Wonderful.. I will bookmark your blog and take the feeds also…I am satisfied to find so much useful info here in the post. Thank you for sharing. Vogue beautiful and popular dancing party full dress, you are worth owning.
http://www.hermeshandbagoutlet.com
http://www.handbagsdreams.com
http://www.backpackunion.com
http://www.charmhandbags.com
http://www.pursehandbag.org

Name: Sgt.Kabuき뙙kiman싒ᴷ 2012-05-28 19:38

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List