c++ - 无法推断模板参数

标签 c++ map comparison key

我正在实现的类的一部分如下所示:

    struct Cord
    {
        int x_cord;
        int y_cord;
        Cord(int x = 0,int y = 0):x_cord(x),y_cord(y) {}
        bool operator()(const Cord& cord) const
        {
            if (x_cord == cord.x_cord)
            {
                return y_cord < cord.y_cord;
            }
            return x_cord < cord.x_cord;
        }
    };
class Cell
    {

    };
std::map<Cord,Cell> m_layout;

我无法编译上面的代码

error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const Layout::Cord'

有什么建议吗?

最佳答案

你的 operator()应该是 operator< :

    bool operator<(const Cord& cord) const
    {
        if (x_cord == cord.x_cord)
        {
            return y_cord < cord.y_cord;
        }
        return x_cord < cord.x_cord;
    }

operator<是什么std::map用于订购其 key 。

关于c++ - 无法推断模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15204422/

相关文章:

c++ - 我的 CMakeLists.txt 文件有什么问题?

c++ - hana简单折叠 "call to non-constexpr function"

list - 是否有相当于 Ruby 的 #map 的 Groovy?

c - 如何让我的 C 程序识别 "\n"字符并忽略它们

Javascript 数组 - 检查两个对象数组的相同内容,忽略顺序

c++ - Crypto++ 中的蛇实现

c++ - 命名空间 C++ 中的外部变量

java - 如何转换/翻译信息?

java - 如何使用两个迭代器遍历 HashMap?

javascript - 是否有更紧凑的选项来检查变量是否等于 "this"或 "that"?