【问题标题】:error C2678: no operator found which takes a left-hand operand of type 'const _Ty'错误 C2678:未找到采用“const _Ty”类型的左操作数的运算符
【发布时间】:2021-05-18 13:54:29
【问题描述】:

我得到错误:

1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\xstddef(127,1): error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion) 当我运行我的代码时:

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstring>
#include <queue>
#include <math.h>

using namespace std;

class grid {
private:
public:
    vector<vector<short> > tiles;

    grid()
    {
        tiles.resize(4);
        for (int i = 0; i < 4; i++)
            tiles[i].resize(4);
    }

    inline bool operator==(grid& b)
    {
        for (int i = 0; i < 4; i++)
            for (int j = 0; j < 4; j++)
                if (tiles[i][j] != b.tiles[i][j])
                    return false;
        return true;
    }

    inline bool operator<(const grid& b)
    {
        return false;
    }

    void shiftUp()
    {
        for (int k = 0; k < 4; k++)
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 3; j++)
                    if (tiles[j][i] == 0)
                        swap(tiles[j][i], tiles[j + 1][i]);

        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                if (tiles[j][i] == tiles[j + 1][i])
                {
                    tiles[j][i] *= 2, tiles[j + 1][i] = 0;
                    break;
                }
            }
        }

        for (int k = 0; k < 4; k++)
            for (int i = 0; i < 4; i++)
                for (int j = 0; j < 3; j++)
                    if (tiles[j][i] == 0)
                        swap(tiles[j][i], tiles[j + 1][i]);
    }
};

map<grid, bool> used;

bool exists(grid x)
{
    return used[x];
}

int main()
{
    grid x;
    for (int i = 0; i < 4; i++)
        for (int j = 0; j < 4; j++)
            cin >> x.tiles[i][j];

    x.shiftUp();
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
            cout << x.tiles[i][j] << " ";
        cout << endl;
    }
}

我正在使用 Visual Studio Community 2019。我尝试将虚拟“

【问题讨论】:

  • 不相关:inline 对于类定义中定义的函数是隐式的,所以你的是不必要的。 (它与函数调用的内联无关。)
  • 您的比较运算符不是 const,但它必须是。
  • operator&lt; 应该是 constoperator== 也是如此)。

标签: c++ c++17


【解决方案1】:

std::map 默认使用std::less 进行比较,默认使用operator &lt;。所以你实现那个操作符是正确的。

但是,错误表明没有operator &lt; 采取const 左侧。你需要一个 const 版本:

class grid {
    // ...
    bool operator < (const grid &o) const { // Note *const* at end
        // Do something meaningful here
    }
}

了解 const 正确性(例如 here)。

还要注意操作符a < b == true,就一定是b &lt; a == false

【讨论】:

  • map 只需要严格的弱排序,不需要全排序。
  • @molbdnilo 那是我不确定的事情,已修复。但是return false肯定是错的
  • 这是最弱的排序。这意味着没有元素在任何其他元素之前排序——换句话说,所有元素都是等价的。这在形式上并没有错,但通常不是很有用。 return true 是非常错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多