2017-07-21 6 views
-2

私はベクトルを作成し、そのベクトルを持つクラスコンストラクタを入力は、それがstd :: vectorを呼び出して使用していますが、std :: vector <double、std :: allocator <double>>が使用されています

undefined reference to Class::Class(std::vector<double, std::allocator<double> >) 

クラスは

std::vector<double> 

のコンストラクタを持っていなく

std::vector<double, std::allocator<double> > 
というエラーを返します。

いくつかのコード例:任意の洞察力が高く評価され

std::vector<double> x1; 
std::vector<double> x2; 
std::vector<double> myvec; 
double thisX; 

for (int i=0; i<x1.size(); i++ { 
    thisX = x1[i]; 
    myvec.push_back(thisX); 
} 
for (int i=0; i<x2.size(); i++ { 
    thisX = x2[i]; 
    myvec.push_back(thisX); 
} 

Class MyClass(myvec); 
// has a constructor for std::vector<double> 
// compiler throws the undefined reference to 
// Class::Class(std::vector<double, std::allocator<double> >) 

!私はそれが明らかでないならば、C++には比較的新しいです。

+0

リンカーは、 'std :: vector 'をとるコンストラクタの定義を見ませんでした。 'std :: vector'は2つのテンプレートパラメータを持つ* class-template *です。要素型、およびアロケータ型。アロケータはデフォルトのテンプレートパラメータ – WhiZTiM

+2

です[docs](http://en.cppreference.com/w/cpp/container/vector)をご覧ください。ベクトルには常に2番目のテンプレートパラメータがあります。デフォルトは 'std :: allocator ' – user463035818

+0

です。コンストラクタはどこで定義されていますか? – NathanOliver

答えて

0

std::vector<double>は、の2番目のテンプレート引数のデフォルトがstd::allocator<T>であるため、std::vector<double, std::allocator<double> >という短い形式です。

エラーの原因は異なります。


undefined reference to Class::Classエラーは、このコンストラクタの宣言が、無定義があることを意味します。

の定義Class::Classであるオブジェクトファイルまたはライブラリをリンクしているとは限りません。その定義されていない参照を解決するにはリンクする必要があります。