【问题标题】:Function for finding in map, overloading operator in map在地图中查找的功能,在地图中重载运算符
【发布时间】:2021-04-14 15:25:54
【问题描述】:

我有类vector2d。我制作 struct comp。这是 Vector2d.h 文件

    class Vector2d{
    int x;
    int y;

public:
    Vector2d(int x, int y): x(x), y(y) {}
    int getX() const{return x;}
    int getY() const{return y;}
    void setX(int x){this->x = x;}
    void setY(int y){this->y = y;}

    bool operator<(const Vector2d &ob) const;
     bool Vector2d::operator==(const Vector2d &ob) const;


};

struct comp
{

    bool operator()(const Vector2d &lhs, const Vector2d &rhs) const
    {
        if (lhs.getX() == rhs.getX())
            return lhs.getY() > rhs.getY();
 
        return lhs.getX() < rhs.getX();
    }
};

Vector2d.cpp文件

#include "Vector2d.h"

 bool Vector2d::operator<(const Vector2d &ob) const
    {
        return x < ob.x || (x == ob.x && y < ob.y);
    }


 bool Vector2d::operator==(const Vector2d &ob) const
    {
        return x == ob.x && y == ob.y;
    }

Piece.h文件

#include <map>
#include <iostream>
#include "Vector2d.h"
enum Name { KING, QUEEN, BISHOP, KNIGHT, ROOK, PAWN, EMPTY };
enum Color { WHITE, BLACK, NONE };

class Piece{
protected:
    Name name;
    Color color;
    Vector2d position;
    
public:
    Name getName(){return this->name;}
    void setName(Name name){this->name = name;}
    Color getColor(){return this->color;}
    void setColor(Color){this->color = color;}
    Vector2d getPosition(){return this->position;}
    void setPosition(Vector2d position){this->position = position;}
    virtual bool move(Vector2d thatPosition, std::map<Vector2d, Piece> Pieces) = 0; 
};

我尝试在 Movement.cpp 文件中的地图上调用 find() 方法

#include "Movement.h"

bool ifLegalDiagonal(Vector2d fromPosition, Vector2d toPosition, std::map<Vector2d, Piece, comp>& pieces)
{
    int fromX = fromPosition.getX();
    int fromY = fromPosition.getY();
    int toX = toPosition.getX();
    int toY = toPosition.getY();

    int lenX = toX - fromX;
    int lenY = toY- fromY; 

    if(abs(lenX) == abs(lenY))
    {
        int xDir = lenX / abs(lenX);
        int yDir = lenY / abs(lenY);

        for (int i = 1; i < abs(lenX); i++)
        {   
        
            Name name = pieces.find(new Vector2d(fromX + xDir*i, fromY + yDir*i)).getName(); //there is a problem
            if(name == EMPTY)
            {

            }
        }
    }
}

VS 代码告诉我这个错误

没有重载函数实例“std::map<_key _tp _compare _alloc>::find [with _Key=Vector2d, _Tp=Piece, _Compare=comp, _Alloc=std::allocator<:pair vector2d piece>>]" 匹配参数列表——参数类型为:(Vector2d *)——对象类型为: std::map>>

我现在不知道缺少什么

【问题讨论】:

    标签: c++ dictionary stl find overloading


    【解决方案1】:

    你错过了两件事:

    class Vector2d{
    
       // ...
    
       bool operator<(const Vector2d &ob) const;
    

    由于这个类已经实现了&lt; 重载,因此不需要std::map 消耗的自定义比较器对象,并且除了这个&lt; 重载本身所做的事情之外,它并没有真正完成更多(除非您需要以非常特定的顺序迭代 map,而显示的代码并非如此)。您可能会完全摆脱它。但更多的是您询问的实际问题:

       pieces.find(new Vector2d ...);
    

    这个pieces 映射是Vector2d 对象的映射。它不是指向Vector2D 对象的指针映射。因为它是Vector2d 对象的映射,所以传递给find() 的参数也必须是Vector2d 对象。毕竟,如果这是ints 的映射,您需要将int 而非int * 传递给find()。仅仅因为地图的键是一个对象而不是普通的 int 并不会改变 find() 的工作方式。

    但是,new 在动态范围内创建一个新对象并为您提供指向它的指针,这就是您传递给find() 的内容。即使这样有效,这也是内存泄漏。

    只需摆脱new

    【讨论】:

    • 我删除新的并且它有效。感谢您解决我的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-05-08
    • 2021-01-06
    • 2012-05-08
    • 2014-09-05
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多