我試圖總結在lambda表達式幾個構造函數,以便存儲那些向量,然後遍歷它:C++載體 - 未定義的引用
using move_constructor = std::function<Move*(Site,vector<Move*>*, int)>;
vector<move_constructor> diff_plane_list = {
[](Site p_site, vector<Move*>* move_list, int latLen) {
return new Diffuse_inplane_px(p_site, move_list, latLen);
},
[](Site p_site, vector<Move*>* move_list, int latLen) {
return new Diffuse_inplane_mx(p_site, move_list, latLen);
},
...
}
然而,嘗試使用G ++編譯它的時候 - 5.3,我得到每個項目的以下錯誤消息中的向量:
/tmp/ccc2A87Z.o: In function `{lambda(std::tuple<int, int, int>, std::vector<Move*, std::allocator<Move*> >*, int)#1}::operator()(std::tuple<int, int, int>, std::vector<Move*, std::allocator<Move*> >*, int) const':
All_moves.cc:(.text+0x5e): undefined reference to `Diffuse_inplane_px::Diffuse_inplane_px(std::tuple<int, int, int>, std::vector<Move*, std::allocator<Move*> >*, int)'
所以vector<Move*>*
被莫名其妙地轉換爲vector<Move*, std::allocator<Move*> >*
? 我應該如何正確地做到這一點?有沒有更好的方法來循環構造函數列表並將指針存儲到向量中創建的對象?
*「未定義的參考」*:您不提供指定的'Diffuse_inplane_px'構造函數的定義。 – Jarod42
順便說一句,最好是返回'std :: unique_ptr'而不是原始擁有指針。 (並且創建你的'make_unique',因爲你沒有C++ 14)。 –
Jarod42
請提供[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) –