Reputation:
I have this code
void Field::tick() {
this->snake.tick(this);
}
where this->snake
is a class property of the Snake
class.
The Snake
class takes this
parameters in the tick()
method:
void tick(Field field);
Of course, in the header file of Snake
, I imported Field.h
.
Now the problem is upon passing an instance of this
in the field class to the tick()
method in Snake
, I get the following error:
c:/Users/x/Documents/NetBeansProjects/snake/Snake.h:12:15: fout: Field has not been declared c:/Users/x/Documents/NetBeansProjects/snake/Field.cpp: In memberfunction void Field::tick(): c:/Users/x/Documents/NetBeansProjects/snake/Field.cpp:14:27: fout: no matching function for call to Snake::tick(Field&) c:/Users/x/Documents/NetBeansProjects/snake/Field.cpp:14:27: note: candidate is: c:/Users/x/Documents/NetBeansProjects/snake/Snake.h:12:10: note: void Snake::tick(int) c:/Users/x/Documents/NetBeansProjects/snake/Snake.h:12:10: note: no known conversion for argument 1 from Field to int
Any suggestions?
Upvotes: 0
Views: 172
Reputation: 30146
In file Snake.h, declare class Field
before the declaration of class Snake
:
class Field;
class Snake
{
...
void tick(Field* field);
...
};
This should fix the compilation error, because you are using a pointer of class Field
in class Snake
. So the compiler only needs to know that such class exists, but it doesn't need to know anything about the contents of that class. If you were using an instance, then you would get a compilation error, because the compiler would need to know the size of that instance.
Therefore, the alternative option of declaring class Snake
before the declaration of class Field
will not compile, because you are using an instance of class Field
in class Snake
, and the compiler needs to know the size of that instance:
class Snake;
class Field
{
...
Snake snake;
...
};
Upvotes: 1