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 :-).

Thanks,
Jack

Recommended Answers

All 5 Replies

Hey,

Can you post the actual files where the errors are on?

:)

http://en.wikipedia.org/wiki/Circular_dependency
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
{
public:

Output

$ g++ -I. Map.cpp Rabbit.cpp main.cpp 
$ ./a.out
1031313133013013100311011031121221111320202130210233122022230130130031131033003132123330210120011310
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
2102
$

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?

Thanks!

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

rabs[i].eat(&rabs,&WIDTH,&HEIGHT,&day,&food);

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.

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.