Jump to content
  • Advertisement
Sign in to follow this  
all_names_taken

Mutual include puzzle

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

How do I do the following: - I have two classes. Both contain a method which takes as argument an object of the other class - these two classes should be in different files - class A should contain an instance of B Pseudo code:
class A {
  void evilMethod(B notFunny) { notFunny.doSomething(); };
  B cool;
};

class B {
  void evilMethod(A notFunny) { notFunny.doSomething(); };
};



My first attempt:
//in file main.cpp
#include "A.h"
#include "B.h"
int main() {
  return 0;
}

//in file A.h
#ifndef A_H
#define A_H
#include "B.h"
class A {
  void evilMethod(B notFunny) { notFunny.doSomething(); };
  B cool;
};
#endif

//in file B.h
#ifndef B_H
#define B_H
#include "A.h"
class B {
  void evilMethod(A notFunny) { notFunny.doSomething(); };
};
#endif



Fails because the other class can't be found... My second attempt:
//in file main.cpp
#include "A.h"
#include "B.h"
int main() {
  return 0;
}

//in file root.h
class A;
class B;

//in file A.h
#ifndef A_H
#define A_H
#include "root.h"
#include "B.h"
class A {
  void evilMethod(B notFunny) { notFunny.doSomething(); };
  B cool;
};
#endif

//in file B.h
#ifndef B_H
#define B_H
#include "root.h"
#include "A.h"
class B {
  void evilMethod(A notFunny) { notFunny.doSomething(); };
};
#endif



Fails because although the class has been declared, it hasn't been defined... How do I solve this?

Share this post


Link to post
Share on other sites
Advertisement
something like this maybe:
in A.h

#ifndef A_H_
#define A_H_

#include "B.h"

class A {
void evilMethod(B notFunny) { notFunny.doSomething(); };
};

#endif


and in B.h

#ifndef B_H_
#define B_H_

#include "A.h"

class B {
void evilMethod(A notFunny) { notFunny.doSomething(); };
};

#endif

Share this post


Link to post
Share on other sites

// a.h
class B;

class A
{
void evilMethod(B * notFunny);
}

// a.cpp
#include "a.h"
#include "b.h"
void A::evilMethod(B * notFunny)
{
notFunny->doSomething();
}

// b.h
class A;

class B
{
void evilMethod(A * notFunny);
}

// b.cpp
#include "a.h"
#include "b.h"
void B::evilMethod(A * notFunny)
{
notFunny->doSomething();
}

Share this post


Link to post
Share on other sites
@DumitruSorin: No, that didn't work (that's like my first attempt pseudo code above). It first includes A, then defines A_H, then includes B, then defines B_H, then includes A, but A_H is defined so it doesn't read contents of A, then it tries to read the class definition of B, but A is not defined. Then it reads the class definition of A, but it's too late because it already failed above.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!