Member Avatar for j.kelly

I am new to C++ and cant get my program to compile because of #include recursion.

I have 3 classes A, B, C.

A includes B.h and C.h
B includes A.h
C includes A.h and B.h

They include each other because they either need to send messages between themselves.

No inheritance is involved.

I've tried using:
#ifndef
#define
.........
#endif
but because A and B are included twice they dont get included the second time.

Any ideas for a C++ beginner who's spent the last 2 days trying to botch something togeather?

Thanks,
James

Can you give an example of how your headers look instead of just which includes the other?

Member Avatar for j.kelly

As they stand they just recur:

#include "Lift.h"
#include "Control.h"
class Floor
{
public:
    Floor(void);
    ~Floor(void);
private:
    Control control;//to send requests
};

#include "Floor.h"
#include "Lift.h"
class Control
{
public:
    Control();
    ~Control(void);
private:
    Floor floor;//to update floor display
    Lift lift;//to tell lift what to do
};

#include "Control.h"
class Lift
{
public:
    Lift();
    ~Lift(void);
private:
    Control control;//to send requests
};

If you want your classes to use each other, you pretty much need to do something like this using forward declarations and pointers (preferably smart pointers as they make your life easier):

#ifndef FLOOR_H
#define FLOOR_H

#include <memory>

class Control;

class Floor
{
public:
  Floor(void);
  ~Floor(void);
private:
  std::auto_ptr<Control> control;//to send requests
};

#endif
#ifndef CONTROL_H
#define CONTROL_H

#include <memory>

class Floor;
class Lift;

class Control
{
public:
  Control();
  ~Control(void);
private:
  std::auto_ptr<Floor> floor;//to update floor display
  std::auto_ptr<Lift> lift;//to tell lift what to do
};

#endif
#ifndef LIFT_H
#define LIFT_H

#include <memory>

class Control;

class Lift
{
public:
  Lift();
  ~Lift(void);
private:
  std::auto_ptr<Control> control;//to send requests
};

#endif

If you don't want to do that, you would be better off restructuring the classes so that they don't depend on each other:

class Floor
{
public:
  Floor();
  ~Floor();
};

class Lift
{
public:
  Lift();
  ~Lift();
private:
  Floor floor[20]; // 20 story building
};

class Control
{
public:
  Control();
  ~Control();
private:
  Lift lift;
};

Control manipulates Lift through the Lift public interface and Lift manipulates the array of Floor through the Floor public interface. Any messaging is done that way. Of course, I don't know how your design is set up, so this suggestion may not work without significant changes.

Member Avatar for j.kelly

Thanks for your help. That was really giving me a headache

James

write a program which input a series of chracters until the user input .

by useing th recursive function??

can any one help

commented: Hmm. Nope. +0
commented: I don't think so... :( +0
commented: Don't bump old threads -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.