C++ passing "this" instance to property in class
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;
...
};
Related videos on Youtube
Alberto
Updated on November 26, 2022Comments
-
Alberto 12 months
I have this code
void Field::tick() { this->snake.tick(this); }
where
this->snake
is a class property of theSnake
class. TheSnake
class takesthis
parameters in thetick()
method:void tick(Field field);
Of course, in the header file of
Snake
, I importedField.h
.Now the problem is upon passing an instance of
this
in the field class to thetick()
method inSnake
, 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?
-
RamblingMad over 9 yearsThat first error indicates that you do not, in fact, include the
Field.h
header file (or not correctly) -
tachomi about 8 yearsWhat are the permissions of the directory you're linking the file?
-
lese about 8 years1. how did you created the link? 2. what are the permissions of destination directory (the one you are pointing with your link) 3. what user are you using while performing your commands?
-
PinkFloyd about 8 yearsdo you want to remove a link that points to a directory ? try
rm work
without the trailing/
-
Alberto about 8 yearsthanks for comments. I used setenforce 1 and then i'm able to get into the folder
-
-
RamblingMad over 9 yearsHe said that he imports the
Field
header file in theSnake
one -
el aurens over 9 yearsI think there is a circular dependency between Field and Snake, so he needs to do forward declaration as Barak Manos suggested
-
barak manos over 9 years@CoffeeandCode: But class
Field
has a member of typeSnake
, hence, both classes need to know of each other. Hence, at least one of them should use an "empty" declaration such asclass Field;
(it cannot beclass Snake;
in file Field.h, because that one uses an actual instance of typeSnake
). -
RamblingMad over 9 years@elaurens yes, but the circular dependency is the other way around than this answer suggests (if
Field
is properly defined inField.h
) -
Admin over 9 yearsThanks! That fixed it. One question though, is it considered good practice to have circular dependencies? Or does it just lead to more problems?
-
barak manos over 9 years@Raoul Van den Berge: You're welcome. Please see updated answer for a brief explanation.
-
Admin over 9 yearsI'm having still a problem with the Snake property though, the property "Snake snake;" in Field is giving the error that it "does not name a type", even though I included Snake.h.
-
barak manos over 9 years@Raoul Van den Berge: I suggest that you add the relevant parts of your code (at least the declaration of both classes) to the question.
-
Admin over 9 yearsSure, no problem. See this: gist.github.com/rvandenberge/ac9471337f349d3acc24
-
barak manos over 9 years@Raoul Van den Berge: You need to use
#ifndef\#define\#endif
in each one of your header files!!! -
Admin over 9 yearsDoesn't #pragma once do the same thing?
-
barak manos over 9 years@Raoul Van den Berge: Well it does, but it's not a part of the standard, so I'm not sure whether or not it's supported on your platform (preprocessor/compiler).
-
barak manos over 9 years@Raoul Van den Berge: Also, why do you
#include "Snake.h"
inside file Snake.h itself??? -
Admin over 9 yearsMy mistake! Seems to be fixed now, thanks for the explanation.