中級 › error: no matching function for call to 'Class::Class()' の原因と直し方 › パターン2
error: no matching function for call to 'Class::Class()' の原因と直し方
引数ありのコンストラクタだけを自分で定義したクラスに対して、引数なしでインスタンスを作ろうとしたときに出るエラーです。
1 #include <iostream> 2 class Rect { 3 public: 4 Rect(int w) { width = w; } 5 int width; 6 }; 7 int main() { 8 Rect r____; ^ 9 std::cout << r.width << std::endl; 10 return 0; 11 }
main.cpp:8:10: error: no matching function for call to 'Rect::Rect()'
空欄を埋めてコンパイル・実行を通す
「コンストラクタを書いても書かなくても引数なしで作れる」というのは誤解です。自分で1つでも定義すると暗黙のデフォルトコンストラクタは消えます。
次の問題