简体   繁体   English

无法推断模板参数

[英]could not deduce template argument

Part of the class I am implementing looks like this: 我正在实现的类的一部分看起来像这样:

    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;

I can`t compile the code above getting 我无法编译上面的代码

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'

Any advices? 有什么建议吗?

Your operator() should be operator< : 你的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< is what std::map uses to order its keys. operator<std::map用来命令其键的内容。

You could fix this by providing an operator<(const Cord&, const Cord&) : 您可以通过提供operator<(const Cord&, const Cord&)来解决此问题:

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

or re-naming operator()(const Cord& cord) const to operator<(const Cord& cord) const 或重命名operator()(const Cord& cord) constoperator<(const Cord& cord) const

You are using your class in a map and it needs to define operator< for it. 您正在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;
}
// ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM