2017-04-26 51 views
0

我試圖總結在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*> >*? 我應該如何正確地做到這一點?有沒有更好的方法來循環構造函數列表並將指針存儲到向量中創建的對象?

+0

*「未定義的參考」*:您不提供指定的'Diffuse_inplane_px'構造函數的定義。 – Jarod42

+1

順便說一句,最好是返回'std :: unique_ptr '而不是原始擁有指針。 (並且創建你的'make_unique',因爲你沒有C++ 14)。 – Jarod42

+0

請提供[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve) –

回答

2

「未定義的參考」:您不提供指定的Diffuse_inplane_px構造函數的定義。

提供一個並確保它與項目鏈接。

+0

是的,gcc希望我指定一個構造函數'Diffuse_inplane_px(std :: tuple , std :: vector > *,int)',我不明白爲什麼第二個參數必須是vector > *,不是'矢量 *'。 lambda的參數是這種類型的,它也在'move_constructor'的定義中指定。使用此類型作爲第二個參數的構造函數在不同的鏈接文件中指定。 – MetalOn

+0

'std :: vector'具有默認的模板參數,因此'vector '與'vector >'相同。你只需要提供'Diffuse_inplane_px(Site,std :: vector *,int)'。 – Jarod42