我有一個包含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
如果您嘗試'Foo ff(costs)'? –
WhozCraig
@WhozCraig,錯誤仍然存在。有任何想法嗎? – Tin
如果你改變它,同樣的錯誤不可能存在,因爲一旦這個改變實際完成,代碼中的任何地方都沒有提到'float'。 [它適用於所有類型正確對齊](http://ideone.com/2G16xx),所以我不知道你的問題是什麼。一旦代碼*有效*(即語法錯誤被刪除),它*工作*。 – WhozCraig