首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法推导模板参数

无法推导模板参数
EN

Stack Overflow用户
提问于 2013-03-04 22:57:32
回答 3查看 1.1K关注 0票数 3

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

代码语言:javascript
复制
    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;

我不能编译上面的代码

代码语言:javascript
复制
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'

有什么建议吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-04 22:58:42

您的operator()应为operator<

代码语言:javascript
复制
    bool operator<(const Cord& cord) const
    {
        if (x_cord == cord.x_cord)
        {
            return y_cord < cord.y_cord;
        }
        return x_cord < cord.x_cord;
    }

std::map使用operator<对其密钥进行排序。

票数 8
EN

Stack Overflow用户

发布于 2013-03-04 22:59:14

您可以通过提供一个operator<(const Cord&, const Cord&)来修复这个问题

代码语言:javascript
复制
// uses your operator()
bool operator<(const Cord& lhs, const Cord& rhs) { return lhs(rhs);)

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

票数 2
EN

Stack Overflow用户

发布于 2013-03-04 23:01:06

您正在map中使用您的类,并且它需要为它定义operator<

代码语言:javascript
复制
// ...
bool operator<(const Cord& cord) const
{
  if (x_cord == cord.x_cord)
    return y_cord < cord.y_cord;
  return x_cord < cord.x_cord;
}
// ...
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15204422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档