2013-08-30 40 views
-2

我有一個包含std::vector<std::vector<T> >屬性的類。在參數化的構造函數中,我使用了move-semantics。 當我創建此類的對象時,出現與constructor相關的編譯器錯誤。如果使用move-semantics正確完成初始化,是否有人有一個想法?或者它實際上與vector<vector>本身有關?在參數化構造函數中使用移動semantincs

template < class T, class L = size_t > 
class Foo{ 
    public: 
    ... 
    Foo(std::vector<L> , std::vector<std::vector<T> >); 
    ... 
    private: 
    ... 
    std::vector<L> shape_; 
    std::vector<std::vector<T> > cost_; 
    ... 

};

template < class T, class L > 
Foo<T,L>::Foo(std::vector<L> shape, std::vector< std::vector<T> > ucosts) 
:shape_(std::move(shape)), cost_(std::move(ucosts)) 
{ 

} 

而這裏的路上,我如何初始化對象:

typedef double termType; 
typedef Foo<termType, int> myFoo; 
std::vector<int> ushape(10); 
std::vector< std::vector< termType> > ucosts(2, std::vector<termType> (5, 0)); 
myFoo ff1(ushape, ucosts); // <------ DOES NOT WORK 
Foo<termType, int> ff2(ushape, ucosts); // <------ DOES WORK 

編譯器消息的錯誤是:'錯誤C2664:

'Foo<T,L>::Foo(std::vector<_Ty>,std::vector<std::vector<double>>)' : cannot convert 
parameter 2 from 'std::vector<_Ty>' to 'std::vector<_Ty>' 
1>   with 
1>   [ 
1>    T=termType, 
1>    L=int, 
1>    _Ty=int 
1>   ] 
1>   and 
1>   [ 
1>    _Ty=std::vector<float> 
1>   ] 
1>   and 
1>   [ 
1>    _Ty=std::vector<double> 
1>   ] 
1>   No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 
+0

如果您嘗試'Foo ff(costs)'? – WhozCraig

+0

@WhozCraig,錯誤仍然存​​在。有任何想法嗎? – Tin

+0

如果你改變它,同樣的錯誤不可能存在,因爲一旦這個改變實際完成,代碼中的任何地方都沒有提到'float'。 [它適用於所有類型正確對齊](http://ideone.com/2G16xx),所以我不知道你的問題是什麼。一旦代碼*有效*(即語法錯誤被刪除),它*工作*。 – WhozCraig

回答

1

termTypedouble,但Foo的模板參數是float。這意味着在ctor中,您試圖將std::vector<double>轉換爲std::vector<float>,這當然是不可能的。

編輯: 實際上,錯誤,即使在移動之前發生的 - 你試圖一個std::vector<double>作爲一個std::vector<float>參數一個參數,而這是不可能的要麼。

+0

@ Angew,當我寫這篇文章時,我確實犯了一個錯誤(兩種類型都是'double')。我編輯它,實際上問題仍然存在。 – Tin

+0

@Tin錯誤訊息清楚地表明它不能從'std :: vector >'轉換爲'std :: vector >'。另外,它提到了'Foo'的雙參數構造函數。你是否在問題中顯示相關代碼? – Angew

+0

@ Angew,現在更新。你介意解釋爲什麼在創建對象'ff1'而不是'ff2'時出現問題? – Tin