2016-05-22 26 views
1

私は、この件に関するすべての投稿を読んでいますが、私が間違っていることを理解することはできません。クラスのメンバ変数である2次元ベクトルを正常に初期化できません。ヘッダファイルです:私のC++クラスで2dベクトルを初期化できません

class Beam2 
    { 
    private: 
     /*The following unit vectors should never be accessed directly 
     and are therefore private.*/ 
     std::vector<std::vector<double> > unitVectors; 

    public: 
    //constructor 
    Beam2(
     Node * node1PtrInput, Node * node2PtrInput, 
     double orientAngleInput); 

私のcppファイル

Beam2::Beam2(
Node * node1PtrInput, Node * node2PtrInput, double orientAngleInput){ 
node1Ptr = node1PtrInput; 
node2Ptr = node2PtrInput; 
orientAngle = orientAngleInput; 
unitVectors(3, std::vector<double>(3)); 
updateUnitVectors(); 

はエラーではありません:エラー: '(のstd ::ベクトル>)(int型、のstd ::ベクトル)' への呼び出しのための一致 unitVectors(3、std :: vector(3)); ^ 助けていただければ幸いです。ここで

答えて

4

は、クラスを初期化する適切な方法である:

Beam2::Beam2(Node * node1PtrInput, Node * node2PtrInput, double orientAngleInput) : 
node1Ptr(node1PtrInput), 
node2Ptr(node2PtrInput), 
orientAngle(orientAngleInput), 
unitVectors(3, std::vector<double>(3)) 
{ 
    updateUnitVectors(); // I believe this is function in the class 
} 

ます。また、あまりにもunitVectors.resize(3, std::vector<double>(3));unitVectors(3, std::vector<double>(3));を交換することによって、あなたのコードを修正し、前者を好むことができます。

+0

お返事ありがとうございます。だからなぜエラーが投げられたのですか?私は、構文は異なるものの、文は同等であると思います。 – BPoy

+0

本体にコンストラクタを入力すると、 'unitVectors'はすでにdefaultに初期化されています。したがって、 'unitVectors(3、std :: vector (3));'のように再度初期化することはできません。サイズを変更する必要があります。 –

関連する問題