【问题标题】:Array as map key数组作为映射键
【发布时间】:2020-02-04 11:50:03
【问题描述】:

我正在尝试使用数组作为映射键。我让它工作了,但是一旦我尝试将它集成到一个类中,我就会收到以下编译器错误:

在“T& Matrix::operator[](std::array) [with T = double]”的实例化中: 35:12:从这里需要 20:24:错误:不匹配调用‘(std::map, double, std::less >, std::allocator, double> > >) (const std::array&)’ 返回矩阵(索引);

这是我的代码的样子:

#include <map>

template <typename T>
struct Matrix{
    std::map<std::array<int,2>,T> matrix;
    int rows;
    int columns;

    Matrix()
    : rows(0),
      columns(0)
    {  }

    Matrix(int rows,int columns)
    :  rows(rows),
    columns(columns)
    { }

    T& operator[](const std::array<int,2> index){
    return matrix(index);
}

    T& operator[](const std::array<int,2>& index) const{
    return matrix(index);
}

};

int main(int argc, char *argv[])
{
    Matrix<double> M(10,10);
    double a = 10;
    M[{10,11}] = a;


    return 0;
}

【问题讨论】:

  • 您还在地图上调用 operator() (std::array&lt;int ,2&gt;),而不是 at(std::array&lt;int, 2&gt;)(在您的 operator[] 函数中)。
  • 为什么要使用array 而不是tuple

标签: c++ arrays c++11 maps operator-overloading


【解决方案1】:

在这些运算符中

T& operator[](const std::array<int,2> index){
return matrix(index);
}

T& operator[](const std::array<int,2>& index) const{
return matrix(index);
}

您正在尝试调用类模板 std::map 的不存在的运算符函数

matrix(index)

很明显你的意思是第一个下标运算符

matrix[index]

T& operator[](const std::array<int,2> &index){
return matrix[index];
}

在第二个下标运算符中是成员函数。

const T& operator[](const std::array<int,2>& index) const{
return matrix.at(index);
}

另外,第二个运算符应该用限定符 const 限定的返回类型声明,即它应该返回一个常量引用,因为成员函数又是一个常量函数。

【讨论】:

  • @ThomasSablik:应该使用at 而不是operator[]
  • @ThomasSablik 哦,我忘了下标运算符如果不存在则插入数据。:)
  • 感谢这帮了大忙!
【解决方案2】:

正如错误信息所说,问题就在这里:

    return matrix(index);

应该是:

    return matrix[index];

注意[] 运算符。

此外,matrix[index] 也不能在 const 上下文中调用,因为在 C++ 中 map::operator[] 会创建缺失元素,因此是潜在的变异操作。我会重新考虑你的设计,否则你的非常量 []const [] 在丢失键方面的行为会有所不同。

注意:你也应该#include &lt;array&gt;


附: std::arrayis provided 的比较运算符。

【讨论】:

    【解决方案3】:

    你的代码有一些问题:

    • array 缺少包含(适用于 gcc,但不适用于 clang)
    • 缺少呼叫操作员的使用
    • 使用副本而不是 const 引用
    #include <array> // Added include
    #include <map>
    
    template <typename T>
    struct Matrix{
        std::map<std::array<int,2>,T> matrix;
        int rows;
        int columns;
    
        Matrix()
        : rows(0),
          columns(0)
        {  }
    
        Matrix(int rows,int columns)
        :  rows(rows),
        columns(columns)
        { }
    
        T& operator[](const std::array<int,2> &index){
            return matrix[index]; // replace call operator
        }
    
        const T& operator[](const std::array<int,2> &index) const{ //return const reference
            return matrix.at(index); // replace call operator
        }
    
    };
    
    int main(int argc, char *argv[])
    {
        Matrix<double> M(10,10);
        double a = 10;
        M[{10,11}] = a;
    
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      • 2015-05-28
      • 2018-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多