![](/img/trans.png)
[英]Binary 'operator' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion)
[英]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
。
通常,每當您看到一條錯誤消息,指出缺少某些內容時,請查看是否存在所有必需的包含文件。 該消息可能是“找不到操作員”,“未定義的類”或“類型不完整”或其他。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.