c++ “Incomplete type not allowed” error accessing class reference information (Circular dependency with forward declaration)

后端 未结 3 566
南方客
南方客 2020-12-28 11:55

Had some issues in my code recently surrounding what I now know of as a Circular dependency. In short there are two classes, Player and Ball, which both need to use informat

3条回答
  •  醉梦人生
    2020-12-28 12:43

    Player.cpp require the definition of Ball class. So simply add #include "Ball.h"

    Player.cpp:

    #include "Player.h"
    #include "Ball.h"
    
    void Player::doSomething(Ball& ball) {
        ball.ballPosX += 10;                   // incomplete type error occurs here.
    }
    

提交回复
热议问题