[英]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.