中級 › error: no matching function for call to 'Class::Class()' の原因と直し方 › パターン3
error: no matching function for call to 'Class::Class()' の原因と直し方
引数ありのコンストラクタだけを自分で定義したクラスに対して、引数なしでインスタンスを作ろうとしたときに出るエラーです。
1 #include <iostream> 2 class Player { 3 public: 4 Player(int hp) { health = hp; } 5 int health; 6 }; 7 int main() { 8 Player p_____; ^ 9 std::cout << p.health << std::endl; 10 return 0; 11 }
main.cpp:8:12: error: no matching function for call to 'Player::Player()'
空欄を埋めてコンパイル・実行を通す
g++は候補一覧を出してくれますが、いずれも「0個の引数では呼べない」ことを伝えています。
次の問題