C++ passing "this" instance to property in class

165

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;
    ...
};
Share:
165

Related videos on Youtube

Alberto
Author by

Alberto

Updated on November 26, 2022

Comments

  • Alberto
    Alberto 12 months

    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?

    • RamblingMad
      RamblingMad over 9 years
      That first error indicates that you do not, in fact, include the Field.h header file (or not correctly)
    • tachomi
      tachomi about 8 years
      What are the permissions of the directory you're linking the file?
    • lese
      lese about 8 years
      1. 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
      PinkFloyd about 8 years
      do you want to remove a link that points to a directory ? try rm work without the trailing /
    • Alberto
      Alberto about 8 years
      thanks for comments. I used setenforce 1 and then i'm able to get into the folder
  • RamblingMad
    RamblingMad over 9 years
    He said that he imports the Field header file in the Snake one
  • el aurens
    el aurens over 9 years
    I think there is a circular dependency between Field and Snake, so he needs to do forward declaration as Barak Manos suggested
  • barak manos
    barak manos over 9 years
    @CoffeeandCode: But class Field has a member of type Snake, hence, both classes need to know of each other. Hence, at least one of them should use an "empty" declaration such as class Field; (it cannot be class Snake; in file Field.h, because that one uses an actual instance of type Snake).
  • RamblingMad
    RamblingMad over 9 years
    @elaurens yes, but the circular dependency is the other way around than this answer suggests (if Field is properly defined in Field.h)
  • Admin
    Admin over 9 years
    Thanks! 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
    barak manos over 9 years
    @Raoul Van den Berge: You're welcome. Please see updated answer for a brief explanation.
  • Admin
    Admin over 9 years
    I'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
    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
    Admin over 9 years
  • barak manos
    barak manos over 9 years
    @Raoul Van den Berge: You need to use #ifndef\#define\#endif in each one of your header files!!!
  • Admin
    Admin over 9 years
    Doesn't #pragma once do the same thing?
  • barak manos
    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
    barak manos over 9 years
    @Raoul Van den Berge: Also, why do you #include "Snake.h" inside file Snake.h itself???
  • Admin
    Admin over 9 years
    My mistake! Seems to be fixed now, thanks for the explanation.