C ++ 11 std :: map程序无法在clang 3.4中编译

时间:2014-05-12 09:11:30

标签: c++ c++11 clang clang++

我是C ++的新手。我试着在clang 3.4中使用" -std = c ++ 11 -stdlib = libc ++"编译一个非常简单的std :: map progeam。旗帜,我得到了我不理解的错误。

#include<map>
#include<string>

template<typename KeyType>
struct ReverseSort {
    bool operator() (const KeyType& key1, const KeyType& key2) {
        return (key1 > key2);
    }
};

int main() {
    using namespace std;
    map<int, string> mapIntToString1;
    map<int, string, ReverseSort<int> > mapIntToString4(mapIntToString1.cbegin(), mapIntToString1.cend());

    return 0;
}

错误是:

map:457:17: error: no matching function for call to object of type 'const ReverseSort<int>'

我知道错误来自main()中的第3行,只是不明白为什么。 相同的程序在g ++ 4.8.2中很好用&#34; -std = c ++ 11&#34;旗帜,我相信它在VC2010中也很不错。

感谢。

1 个答案:

答案 0 :(得分:7)

您的operator()成员必须是const

bool operator() (const KeyType& key1, const KeyType& key2) const
{   //                                                     ^^^^^
    return (key1 > key2);
}
相关问题