簡體   English   中英

二進制“ [”:找不到使用“初始化列表”類型的右側操作數的運算符(或者沒有可接受的轉換)

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

相關問題 二進制&#39;operator&#39;:未找到采用&#39;Fraction&#39;類型的右側操作數的運算符(或沒有可接受的轉換) C2679 二進制“-=”:未找到采用“T”類型右側操作數的運算符(或沒有可接受的轉換) 重載插入運算符:未找到采用“ unsigned int”類型的右側操作數的運算符(或者沒有可接受的轉換) 錯誤C2679:binary&#39;=&#39;:找不到運算符,它接受類型&#39;std :: vector &lt;_Ty&gt; *&#39;的右手操作數(或者沒有可接受的轉換) 錯誤 C2679:二進制“&lt;&lt;”:未找到采用“RatNum”類型的右側操作數的運算符(或沒有可接受的轉換) 錯誤C2679二進制&#39;=&#39;:找不到使用&#39;int&#39;類型的右側操作數的運算符(或者沒有可接受的轉換) 錯誤:C2679二進制&#39;==&#39;:未找到采用類型為&#39;const std :: string&#39;的右側操作數的運算符(或者沒有可接受的轉換 錯誤C2679:二進制&#39;&gt;&gt;&#39;:未找到采用&#39;const char [4]&#39;類型的右側操作數的運算符(或沒有可接受的轉換)22 c ++錯誤c2679:二進制&#39;[&#39;:未找到采用&#39;SalesItem&#39;類型的右側操作數的運算符(或者沒有可接受的轉換) 錯誤C2679:二進制&#39;&gt;&gt;&#39;:找不到哪個運算符采用&#39;std :: string&#39;類型的右手操作數(或者沒有可接受的轉換)
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM