はじめに
C++を書いていてふとしたミスで悩んでしまったので、原因と解決策を記します。
状況
自作クラスdim_3
を持つvector<dim_3>
をresize()
しようとするが、コンパイルエラーが起こる。ソースコードは以下。
# include <iostream>
# include <vector>
using namespace std;
class dim_3 {
private:
int x_;
int y_;
int z_;
public:
dim_3 (int x, int y, int z) : x_{x}, y_{y}, z_{z} {}
inline void setX (int in) { x_ = in; }
inline void setY (int in) { y_ = in; }
inline void setZ (int in) { z_ = in; }
inline int getX () const { return x_; }
inline int getY () const { return y_; }
inline int getZ () const { return z_; }
};
int main(){
vector<dim_3> vc;
vc.resize(10);
cout << vc[4].getX() << " " << vc[4].getY() << " " << vc[4].getZ() << endl;
return 0;
}
error: no matching constructor for initialization of 'dim_3'
dim_3
を初期化するコンストラクタがないらしい。
val
Object whose content is copied to the added elements in case that n is greater than the current container size.
If not specified, the default constructor is used instead.
Member type value_type is the type of the elements in the container, defined in vector as an alias of the first template parameter (T).
と記述されており、std::vector::resize
では値の指定がない時はデフォルトコンストラクターを用いるとのこと。
原因
デフォルトコンストラクターを忘れていた。
解決方法1
デフォルトコンストラクターを作る。
冗長になるが、書き直したソースコードが以下。
# include <iostream>
# include <vector>
using namespace std;
class dim_3 {
private:
int x_;
int y_;
int z_;
public:
dim_3 () : x_{0}, y_{0}, z_{0} {}
dim_3 (int x, int y, int z) : x_{x}, y_{y}, z_{z} {}
inline void setX (int in) { x_ = in; }
inline void setY (int in) { y_ = in; }
inline void setZ (int in) { z_ = in; }
inline int getX () const { return x_; }
inline int getY () const { return y_; }
inline int getZ () const { return z_; }
};
int main(){
vector<dim_3> vc;
vc.resize(10);
cout << vc[4].getX() << " " << vc[4].getY() << " " << vc[4].getZ() << endl;
return 0;
}
実行結果が以下。
0 0 0
解決方法2
初期化方法を指定する。
これは使いやすくないが、試したら動いたので一応記す。これを用いるとしてもデフォルトコンストラクターは作るべきである。ソースコードは以下。
# include <iostream>
# include <vector>
using namespace std;
class dim_3 {
private:
int x_;
int y_;
int z_;
public:
dim_3 (int x, int y, int z) : x_{x}, y_{y}, z_{z} {}
inline void setX (int in) { x_ = in; }
inline void setY (int in) { y_ = in; }
inline void setZ (int in) { z_ = in; }
inline int getX () const { return x_; }
inline int getY () const { return y_; }
inline int getZ () const { return z_; }
};
int main(){
vector<dim_3> vc;
vc.resize(10, dim_3(8,9,0));
cout << vc[4].getX() << " " << vc[4].getY() << " " << vc[4].getZ() << endl;
return 0;
}
実行結果が以下。
8 9 0
おわりに
今後は、デフォルトコンストラクターを忘れないよう注意します。