LoginSignup
0
0

More than 3 years have passed since last update.

std::vector<クラス> の初期化でNo matching constructor for initialization

Posted at

大きいサイズのstd::vectorに値を追加していく際はpush_back()は遅いので,最初に空の配列を用意し,要素代入で初期化してゆくほうが速いとのこと.これをクラスのvectorでやろうと思ったときにハマったのでメモ.

以下のページで解決した.

// Nはサイズ,Student(0)でコンストラクタを呼び出している.
std::vector<Student> vec(N, Student(0));

もしStudentクラスがデフォルトコンストラクタを持っていなければ,

std::vector<Student> vec(N);

とするとコンストラクタが呼び出せずエラーになる.(No matching constructor for initialization of "...")

No matching constructor for initialization of ...のエラーについては下記記事もまとまっていて良い.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0