I have a project going (the idea for which I got from a website a couple of years ago) and its been going pretty well. However, I have come upon a strange error, which I can't find the source of (well, I know the source but I don't know what I'm doing wrong).
I have attached the full source (it concerns simulation of a rabbit colony's life) below.
The error that the compiler gives me is (lines 17 & 18, file Map.h) error: 'Rabbit' has not been declared. Then, on line 23 of the same file: error: 'Rabbit' does not name a type. They must be related...

Please read the offending file, and make a suggestion :-).


4 Years
Discussion Span
Last Post by histrungalot

Look at where it says Example of circular dependencies in C++.

You need the class Map in Rabbit but Rabbit needs class Map. Hence circular dependency.
Fix it with a forward declaration

#ifndef RABBIT_H
#define RABBIT_H

// Remove the #include "Map.h" and use a 
// forward declaration 
class Map;

class Rabbit


$ g++ -I. Map.cpp Rabbit.cpp main.cpp 
$ ./a.out
Creating Living Rabbit 0
2Creating Living Rabbit 1
0Creating Living Rabbit 2
0Creating Living Rabbit 3
Creating Dead Rabbit 93
Creating Dead Rabbit 94
Creating Dead Rabbit 95
Creating Dead Rabbit 96
Creating Dead Rabbit 97
Creating Dead Rabbit 98
Creating Dead Rabbit 99

How do I handle making sure that the function eat(Map*) in the rabbit class knows what Map is, and at the same time let Map know about Rabbit. I understand circular dependancy, so that is not the issue (anymore), but how can I allow both files to know about each other?

Just another question: what does the -I option do in a g++ command?



I've changed the code to not take a pointer to a map, but to a set of pointers to the data values of a map...
in Rabbit.h:

virtual void eat(/*Map* map*/Rabbit *rabs[],const int* WIDTH,const int* HEIGHT,int* day,float* food);

However, when I try to run the method in Map.cpp with


I get this error: error: no matching function for call to 'Rabbit::eat(Rabbit (*)[100], const int*, const int*, int*, float*)'
I can't figure it out. My method asks for a pointer to an array of rabbits, and I give an array of rabbits to it. Does '&' not work for arrays or something? It works for primitives, it seems.

Thanks :-)


Post the description of the project or the link to the description. That way I can see what the overall picture is.

Edited by histrungalot: Fixing

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.