二进制“[”:未找到接受 'initializer list' 类型右手操作数的运算符(或没有可接受的转换)
binary '[': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion)
我在尝试编译我的代码时收到错误消息:
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)'.
我不明白我做错了什么。所以我的问题是出了什么问题以及如何解决它。此外,作业是使用
Matrix<double> M(10, 20);
M[{0, 0}] = 1.0; // set value at row 0, column 0 to 1.0
所以这部分我不能改。
#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;
}
在文件的顶部,#include "stdafx.h"
之后放
#include <array>
或者,您可以将该行放入文件 stdafx.h
。
一般来说,每当您看到一条错误消息说缺少某些东西时,请查看是否所有必需的 include-files 都存在。该消息可能是 "no operator found"、"undefined class" 或 "incomplete type" 或其他内容。
我在尝试编译我的代码时收到错误消息:
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)'.
我不明白我做错了什么。所以我的问题是出了什么问题以及如何解决它。此外,作业是使用
Matrix<double> M(10, 20);
M[{0, 0}] = 1.0; // set value at row 0, column 0 to 1.0
所以这部分我不能改。
#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;
}
在文件的顶部,#include "stdafx.h"
之后放
#include <array>
或者,您可以将该行放入文件 stdafx.h
。
一般来说,每当您看到一条错误消息说缺少某些东西时,请查看是否所有必需的 include-files 都存在。该消息可能是 "no operator found"、"undefined class" 或 "incomplete type" 或其他内容。