中級 › error: no matching function for call to 'Class::Class()' の原因と直し方 › パターン1
error: no matching function for call to 'Class::Class()' の原因と直し方
引数ありのコンストラクタだけを自分で定義したクラスに対して、引数なしでインスタンスを作ろうとしたときに出るエラーです。
1 #include <iostream> 2 class Timer { 3 public: 4 Timer(int seconds) { s = seconds; } 5 int s; 6 }; 7 int main() { 8 Timer t___; ^ 9 std::cout << t.s << std::endl; 10 return 0; 11 }
main.cpp:8:11: error: no matching function for call to 'Timer::Timer()'
空欄を埋めてコンパイル・実行を通す
Timer(int seconds)を自分で定義した時点で、引数なしのデフォルトコンストラクタは自動生成されなくなります。引数を渡して呼び出す必要があります。
次の問題