Why can't the compiler find the superclass's method?
我试图在C++中继承类继承,但是它显然与Python有很大的不同。
现在,我有两个类,一个称为
首先,它的行为就像一个单身汉。它有一个静态功能叫
第二个问题是,它作为一个类工作,用于创建具有播放器编号作为属性的对象。因此,如果用类构造一个对象,那么应该返回一个具有
我希望
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <iostream> #include <string> using namespace std; class TicTacToeGame { }; class Player { public: static void make_move(int player_number, TicTacToeGame& game); protected: int player_number; public: explicit Player(int player_number_param) { player_number = player_number_param; } public: void make_move(TicTacToeGame& game) { return make_move(player_number, game); } }; class HumanPlayer: public Player { public: static void make_move(int player_number, TicTacToeGame& game) {} public: HumanPlayer(int player_number_param): Player(player_number_param) {} }; int main() { TicTacToeGame game; HumanPlayer human_player = HumanPlayer(2); human_player.make_move(game); return 0; } |
我最近了解到,子类不继承构造函数,所以我不得不编写一个新的静态函数和一个构造函数,我已经做过了。但是,每当我初始化一个新的
我收到的具体错误消息是
C:\Users\London\Desktop\Python
Programs\LearningC++\FirstProgram_SO.cpp: In function 'int main()':
C:\Users\London\Desktop\Python
Programs\LearningC++\FirstProgram_SO.cpp:41:29: error: no matching
function for call to 'HumanPlayer::make_move(TicTacToeGame&)'
human_player.make_move(game); ^ C:\Users\London\Desktop\Python
Programs\LearningC++\FirstProgram_SO.cpp:29:15: note: candidate:
static void HumanPlayer::make_move(int, TicTacToeGame&) static void
make_move(int player_number, TicTacToeGame& game) {} ^~~~~
C:\Users\London\Desktop\Python
Programs\LearningC++\FirstProgram_SO.cpp:29:15: note: candidate
expects 2 arguments, 1 provided
我怎样才能让
对同名静态函数的重新定义将隐藏要使用的静态函数。
要么以不同的方式重命名,要么添加
1 2 | public: using Player::make_move; |
注意,与Java不同的是,在每个函数之前不需要重复EDCOX1×0,只要你不改变它,同样的可见性就可以应用。
1 2 3 4 5 6 7 8 | class YourClass { public: void foo1(); // public void bar1(); // also public protected: void foo2(); // protected void bar2(); // also protected }; |