Issue declaring extern class object

后端 未结 4 1095
孤街浪徒
孤街浪徒 2020-12-21 12:37

Let me start by saying I\'ve extensively searched for answers on google and more specifically here.

The thing is I actually (at least I think I did) found people wit

4条回答
  •  感动是毒
    2020-12-21 12:59

    You need to put the definition of your Player class in the header file, before you declare the extern variable. Otherwise the compiler has no idea what Player is.

    I suggest something like this:

    player.h

    #ifndef PLAYER_H_
    #define PLAYER_H_
    
    class Player {
        ...
    };
    
    #endif
    

    player.cpp

    #include "player.h"
    
    Player john;
    

    cc.h

    #ifndef CC_H_
    #define CC_H_
    
    #include "player.h"
    
    extern Player john;
    
    #endif
    

提交回复
热议问题