3

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

    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'

有什么建议吗?

4

3 回答 3

8

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用来订购它的钥匙的。

于 2013-03-04T14:58:42.030 回答
2

您可以通过提供以下内容来解决此问题operator<(const Cord&, const Cord&)

// uses your operator()
bool operator<(const Cord& lhs, const Cord& rhs) { return lhs(rhs);)

或重命名operator()(const Cord& cord) constoperator<(const Cord& cord) const

于 2013-03-04T14:59:13.930 回答
2

您在 a 中使用您的类,map它需要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;
}
// ...
于 2013-03-04T15:01:06.303 回答