-5

Town.h

class Town;

Tailor.h

#include "Town.h"
class Bully;
#include "Bully.h"
class Tailor
{
  public:
    //LookAround is called only in randWalk
    void lookAround(Town & town, const short direction, const Bully bully[]);
    //THIS is the code inside randwalk that calls bully 
    if(!bully[0].punch(*this,town))

    void randWalk(Town & town, const Bully bully[]);
  private:
}

Bully.h

#include "Town.h"
class Tailor;
#include "Tailor.h"
class Bully
{
  public:
    bool punch(Tailor& tailor, Town & town);
  private:
}

main.cpp

#include "Town.h"
#include "Tailor.h"
#include "Bully.h"

int main()
{
  const char PLAYER_NAME[] = "Milhouse";

  Bully bully_team[5];

  //create a town
  Town r(10,40,25);

  //create a Tailor player called PLAYER_NAME and place him at 2,3
  Tailor player(PLAYER_NAME);
  player.placeMe(r);

  //make the character move 12 times
  for(int i = 0; i < 100; i++)
  {
    player.randWalk(r, bully_team);
  }
  return 0;
}

The problem is that I need access to an array of type Bully in a Player function, but Bully needs type Player to function. This Bully array needs to be declared in main. I have been trying many different ways of forwarding classes, but i can't seem to get it work.

In file included from Tailor.h:8:0,
                 from main.cpp:7:
Bully.h:43:17: error: 'MAX_NAME_LENGTH' was not declared in this scope
     char m_name[MAX_NAME_LENGTH];
                 ^
Bully.h: In function 'std::ostream& operator<<(std::ostream&, const Bully&)':
Bully.h:31:33: error: 'const class Bully' has no member named 'm_name'
       int length = strlen(bully.m_name);
                                 ^
Bully.h:35:23: error: 'const class Bully' has no member named 'm_name'
         cout << bully.m_name[i];
                       ^
In file included from Tailor.h:8:0,
                 from Tailor.cpp:1:
Bully.h:43:17: error: 'MAX_NAME_LENGTH' was not declared in this scope
     char m_name[MAX_NAME_LENGTH];
                 ^
Bully.h: In function 'std::ostream& operator<<(std::ostream&, const Bully&)':
Bully.h:31:33: error: 'const class Bully' has no member named 'm_name'
       int length = strlen(bully.m_name);
                                 ^
Bully.h:35:23: error: 'const class Bully' has no member named 'm_name'
         cout << bully.m_name[i];
                       ^
Tailor.cpp: In member function 'void Tailor::lookAround(Town&, short int, const Bully*)':
Tailor.cpp:70:34: error: passing 'const Bully' as 'this' argument discards qualifiers [-fpermissive]
     if(!bully[0].punch(*this,town))
                                  ^
In file included from Tailor.h:8:0,
                 from Tailor.cpp:1:
Bully.h:27:10: note:   in call to 'bool Bully::punch(Tailor&, Town&)'
     bool punch(Tailor& tailor, Town & town);
          ^
Parker
  • 101
  • 1

1 Answers1

1

Use this order:

  1. Declare Player and Bully
  2. Define Player and Bully
  3. Define the functions of Player and Bully and main

The order within 2. may be significant. Only one of Player and Bully can depend on the definition of the other, and that dependency determines which must be defined after the other. The one that doesn't have a dependency must at most depend on the declaration of the other. Declaring a function that takes a pointer to an array does not require the definition of the pointed type, so Player apparently doesn't depend on the definition of Bully.

The order within 1. and 3. is not significant, and each of the functions may be defined in separate translation unit (source file) if you so wish.

eerorika
  • 232,697
  • 12
  • 197
  • 326