Hi, I have a problem with two classes who both has instances to each other.

Here are the header files...


#include <vector>
#include "item.h"
#include "actor.h"

namespace game{
	class Environment{
		Environment(std::string name_, std::string description_);
		enum directions {
			north = 0,
			north_east = 1,
			east = 2,
			south_east = 3,
			south = 4,
			south_west = 5,
			west = 6,
			north_west = 7
		void add_item(Item & it); 
		void remove_item(Item & it);
		void add_exit(const int position, const Environment & env);
		void remove_exit(const int position);
		void add_actor(Actor & act);
		void remove_actor(Actor & act);
		std::string name, description;		
		std::vector<Item> items;
		bool existing_exits[8];
		Environment * exits;
		std::vector<Actor> actors;

#ifndef ACTOR_H
#define ACTOR_H

#include <string>
#include "environment.h"

namespace game{
	class Actor{
		#define max_level 20
		std::string name, description, type;
		int vitality, strength, level, dexterity;
		bool evil;
		Environment current_location;
		const int get_vitality() const{ return vitality; }
		const int get_strength() const{ return strength; }
		const int get_dexterity() const{ return dexterity; }
		const int get_level() const{ return level; }
		const std::string get_name() const{ return name; }
		const std::string get_description() const{ return description; }
		const std::string get_type() const{ return type; }
		const bool is_evil() const{ return evil; }
		const bool operator==(const Actor & ref) const;
		//const Environment & get_location() const{ return current_location; }
		void increase_level(int lev);
		//virtual const bool attack(Actor & other) const = 0;
		//virtual void take_damage(const int damage) = 0;
		const float chance_to_hit(const Actor other) const;


I get the following errors when i compile

wpa-n2-53:lab3 sason$ make
g++ -c actor.cpp
In file included from actor.h:6,
from actor.cpp:2:
environment.h:34: error: ‘Actor’ has not been declared
environment.h:35: error: ‘Actor’ has not been declared
environment.h:44: error: ‘Actor’ was not declared in this scope
environment.h:44: error: template argument 1 is invalid
environment.h:44: error: template argument 2 is invalid
make: *** [all] Error 1

How do i let the class know that there is a "Actor" type?

Circular dependencies. You can only do it by using the two class pointers because otherwise the class must be fully defined before it can be referenced.

class Actor;
class Environment
  // blabla
void add_actor(Actor * act);

std::vector<Actor*> actors;

Edited 7 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.