2012-02-05 29 views
0

我得到一個奇怪的編譯器錯誤,因爲我是新的使用set的自定義結構的我不知道究竟是什麼問題。自定義結構集合聲明中的錯誤

我正在嘗試創建一組「對」,並使用自定義比較函數插入所述對。

struct pairT { 
    std::string first, second; 
}; 

int PairCmp(pairT &one, pairT &two) { 
    if (one.first < two.first && one.second == two.second) return 0; 
    else if (one.first < two.first) return -1; 
    else if (one.first == two.first && one.second < two.second) return -1; 
    return 1; 
} 

std::set<pairT> CartesianProduct (std::set<std::string> &one, std::set<std::string> &two) { 
    std::set<pairT> returnSet(PairCmp); 
    /.../ 

我得到的錯誤從代碼的最後一行:從int爲const的std ::少...等等,等等,等等 C2664「不能轉換參數1

任何建議,爲什麼我得到我的屁股踢?

+0

這回答你的問題:http://stackoverflow.com/questions/2620862/using-custom-stdset-comparator。也可能是重複的。 – kkm 2012-02-05 10:34:24

回答

1

使用對象(而不是指針)要求您命名爲用於比較的pairT兩個對象std::set第二個模板參數。示例見std::less<>

另外,你在這裏嘗試的似乎是錯誤的。您試圖在CartesianProduct()中返回一個std::set,但返回的PairCmp()返回一個整數。

+0

謝謝馬里奧 - 你在說最後一行中的第二個參數嗎?那會是什麼? – MCP 2012-02-05 10:26:37

+0

不,在模板參數列表中。但是如果你爲你的struct重載'operator <',你將不需要一個。但直接的錯誤是我添加到答案的錯誤,你基本上試圖返回一個整數作爲'std :: set'。 – Mario 2012-02-05 10:29:38