简体   繁体   English

二进制“ [”:找不到使用“初始化列表”类型的右侧操作数的运算符(或者没有可接受的转换)

[英]binary '[': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion)

I get an error when trying to compile my code which says: 尝试编译代码时出现错误:

error C2679: binary '[': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion). 
note: could be 'double &Matrix<double>::operator [](const std::array<int,2> &)'. 
note: while trying to match the argument list '(Matrix<double>, initializer list)'. 

I don't understand what I am doing wrong. 我不明白我在做什么错。 So my question is what's wrong and how to fix it. 所以我的问题是出了什么问题以及如何解决。 Also, the assignment is to use 另外,分配是使用

Matrix<double> M(10, 20);
M[{0, 0}] = 1.0; // set value at row 0, column 0 to 1.0

So this part I can't change. 所以这部分我不能改变。

#include "stdafx.h"
#include <iostream>
#include <initializer_list>
#include <memory>
#include <string>
#include <map>


//Matrix Class
template<typename T>
class Matrix {
private:
    int rows;
    int columns;
public:
    std::map< std::array<int, 2>, T > data;
    //Constructor
    Matrix() {
        rows = 0;
        columns = 0;
    }

    Matrix(int rows, int columns)
        : rows(rows), columns(columns)
    {
        //std::clog << "Matrix(int rows, int columns) called" << std::endl;
    }


    //Destructor
    ~Matrix()
    {
        data.clear();
        rows = 0;
        columns = 0;
    }


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


int main()
{
    Matrix<double> M(10, 20);
    M[{0, 0}] = 1.0; // set value at row 0, column 0 to 1.0
    //M[{1, 2}] = 2.0; // set value at row 1, column 2 to 2.0


    std::cout << " Finished!" << M[{0, 0}] << std::endl;


    return 0;
}

At the top of the file, after #include "stdafx.h" put 在文件顶部,在#include "stdafx.h"之后

#include <array>

Alternately, you could put that line into the file stdafx.h . 或者,您可以将该行放入文件stdafx.h

In general, whenever you see an error message saying that something is missing, see if all necessary include-files are present. 通常,每当您看到一条错误消息,指出缺少某些内容时,请查看是否存在所有必需的包含文件。 The message might be "no operator found", "undefined class", or "incomplete type" or something else. 该消息可能是“找不到操作员”,“未定义的类”或“类型不完整”或其他。

暂无
暂无

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

相关问题 二进制&#39;operator&#39;:未找到采用&#39;Fraction&#39;类型的右侧操作数的运算符(或没有可接受的转换) - Binary 'operator' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion) C2679 二进制“-=”:未找到采用“T”类型右侧操作数的运算符(或没有可接受的转换) - C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) 重载插入运算符:未找到采用“ unsigned int”类型的右侧操作数的运算符(或者没有可接受的转换) - Overloading Insertion Operator: no operator found which takes a right-hand operand of type 'unsigned int' (or there is no acceptable conversion) 错误C2679:binary&#39;=&#39;:找不到运算符,它接受类型&#39;std :: vector &lt;_Ty&gt; *&#39;的右手操作数(或者没有可接受的转换) - error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) 错误 C2679:二进制“&lt;&lt;”:未找到采用“RatNum”类型的右侧操作数的运算符(或没有可接受的转换) - error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) 错误C2679二进制&#39;=&#39;:找不到使用&#39;int&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) 错误:C2679二进制&#39;==&#39;:未找到采用类型为&#39;const std :: string&#39;的右侧操作数的运算符(或者没有可接受的转换 - Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion 错误C2679:二进制&#39;&gt;&gt;&#39;:未找到采用&#39;const char [4]&#39;类型的右侧操作数的运算符(或没有可接受的转换)22 - Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 c ++错误c2679:二进制&#39;[&#39;:未找到采用&#39;SalesItem&#39;类型的右侧操作数的运算符(或者没有可接受的转换) - c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) 错误C2679:二进制&#39;&gt;&gt;&#39;:找不到哪个运算符采用&#39;std :: string&#39;类型的右手操作数(或者没有可接受的转换) - error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM