我正在实现的类的一部分如下所示:
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'有什么建议吗?
发布于 2013-03-04 22:58:42
您的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;
}std::map使用operator<对其密钥进行排序。
发布于 2013-03-04 22:59:14
您可以通过提供一个operator<(const Cord&, const Cord&)来修复这个问题
// uses your operator()
bool operator<(const Cord& lhs, const Cord& rhs) { return lhs(rhs);)或将operator()(const Cord& cord) const重命名为operator<(const Cord& cord) const
发布于 2013-03-04 23:01:06
您正在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;
}
// ...https://stackoverflow.com/questions/15204422
复制相似问题