2016-03-19 84 views
1

我有一個用戶定義的類的向量,我想對這個向量進行排序。將會有3個屬性基於哪個排序可以發生。我正在使用lambda函數來捕獲基於哪個向量應該排序的屬性。但是,我收到編譯器錯誤。我粘貼下面的代碼:用lambda函數排序stl容器

mapNode * PhotonMap::createTree(int start, int end) 
{ 
    mapNode *n = new mapNode(); 

    if (end - start == 0) 
    { 
     n->node.sPosition.setVector(pMap[end].sPosition); 
     n->node.pPower.setVector(pMap[end].pPower); 
     n->sAxis = -1; 
     n->left = NULL; 
     n->right = NULL; 
     return n; 
    } 

    BBox box; 

    if (!(end - start + 1 == pMap.size())) 
     box.setBBox(computeBox(start, end)); 

    int splitAxis = decodeSplitAxis(box.getMaxSpreadAxis()); 
    n->sAxis = splitAxis; 

    std::sort(pMap[start], pMap[end], [splitAxis](Photon &first, Photon &second) ->bool { return (first.sPosition.getValue(splitAxis) > second.sPosition.getValue(splitAxis)); }); 

    int mIndex = floor((start + end)/2); 

    n->node.sPosition.setVector(pMap[mIndex].sPosition); 
    n->node.pPower.setVector(pMap[mIndex].pPower); 

    if (mIndex == start) 
    { 
     //this means end - start = 1. There will be no left node! 
     n->left = NULL; 
     n->right = createTree(mIndex + 1, end); 
     return n; 
    } 
    else { 
     n->left = createTree(start, mIndex); 
     n->right = createTree(mIndex + 1, end); 
     return n; 
    } 

} 

我正的錯誤有以下幾種:

錯誤C2784:「未知類型的std ::運營商 - (標準:: move_iterator < _RanIt> & ,常量的std :: move_iterator < _RanIt2> &) ':不能推導出模板參數爲 '的std :: move_iterator < _RanIt> &' 從 '光子'

錯誤C2784:' 未知類型的std ::運營商 - (缺點牛逼的std :: reverse_iterator的< _RanIt> &,常量的std :: reverse_iterator的< _RanIt2> &)「:不能推導出模板參數的 '常量的std :: reverse_iterator的< _RanIt> &' 從 '光子'

錯誤C2676:二進制「 - 」:「光子」不限定此運算符或轉換到類型接受的預定義的運算符

錯誤C2672:「_Sort」:沒有匹配的重載函數發現

錯誤C2780:「無效STD :: _ Sort(_RanIt,_RanIt,_Diff,_Pr)':期望4個參數 - 3提供

Photonstruct。它的聲明是:

typedef struct Photon { 

    Vector sPosition; 
    Vector iDirection; 
    Vector pPower; 
    Vector norm; 

} Photon; 

Vectorclass,其私有成員:

int length; 
void allocateMemory(void); 
float vector[3]; 

回答

1

前兩個參數std::sort()應該是迭代器,在容器中的內容不引用。指針也可以工作,所以:

std::sort(&pMap[start], &pMap[end], /* your lambda goes here */); 

既然你沒有提供Minimum, Complete, and Verifieable example,也可能有其他問題與您的代碼,以防止編譯,但是這至少是他們的第一個。