英文:
C++ error: invalid initialization of reference of type ‘Array<int>&’ from expression of type ‘int’
问题
以下是要翻译的内容:
我正在使用一个模板类Array来实现一个模板类Matrix,它的行为类似于一个二维数组。我有一个函数,它应该获取矩阵的一行。
template<typename T>
class Matrix {
public:
    Matrix(): rows(0), cols(0), data(){}
    Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, Array<T>(cols)) {}
    Array<T>& operator[](int row) {
        return data[row];
    }
    // 其他函数
private:
    int rows, cols;
    Array<Array<T>> data;
};
Array:
template<typename T>
class Array {
public:
    Array(): len{0}, buf{nullptr} {
    }
    explicit Array(int len) : len{len}, buf{new T[len]} {
    }
    // 复制构造函数、移动构造函数、析构函数
    int& operator[](int index) {
        if (!in_bounds(index))
            throw std::string("Exception operator[](" + std::to_string(index) + ") Out Of Range");
        return buf[index];
    }
    const int& operator[](int index) const {
        if (!in_bounds(index))
            throw std::string("Exception operator[](" + std::to_string(index) + ") Out Of Range");
        return buf[index];
    }
private:
    int len; 
    T* buf;
    bool in_bounds(int index) const {
        return index >= 0 && index < len;
    }
};
这行代码return data[row];似乎出现了提到的错误。
我查看了其他解决此错误的方法,但仍然不明白它。我该如何修复这个问题?谢谢!
英文:
I'm using a template class Array to implement a template class Matrix of T that acts like a 2D array. I have a function that is supposed to get a row of the matrix.
template<typename T>
class Matrix {
public:
    Matrix(): rows(0), cols(0), data(){}
    Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, Array<T>(cols)) {}
    Array<T>& operator[](int row) {
        return data[row];
    }
    // other functions
private:
    int rows, cols;
    Array<Array<T>> data;
};
Array:
template<typename T>
class Array {
public:
    Array(): len{0}, buf{nullptr} {
    }
    explicit Array(int len) : len{len}, buf{new T[len]} {
    }
    // copy constructor, move constructor, destructor
    int& operator[](int index) {
        if (!in_bounds(index))
            throw std::string("Exception operator[](" + std::to_string(index) + ") Out Of Range");
        return buf[index];
    }
    const int& operator[](int index) const {
        if (!in_bounds(index))
            throw std::string("Exception operator[](" + std::to_string(index) + ") Out Of Range");
        return buf[index];
    }
private:
    int len; 
    T* buf;
    bool in_bounds(int index) const {
        return index >= 0 && index < len;
    }
};
The line return data[row]; appears to give the error mentioned.
I looked at other solutions to this error and still didn't understand it. How do I fix this? Thanks!
答案1
得分: 1
The Matrix 构造函数使用 rows 和 cols 参数来初始化 data 成员,使用一个接受 int 和 array<T> 的构造函数。
data 被定义为
Array<Array<T>> data;
而 Array 没有接受 int 和 Array 的构造函数。你可以让只接受 len 参数的构造函数也接受一个可选的 T,然后使用它来赋值给 Array:
explicit Array(int len, T copy = {}) : len{len}, buf{new T[len]} {
    std::fill_n(buf, len, copy);
}
你还需要修改 Array::operator[],使其返回对 T 的引用,而不是 int。你的 Array 存储的是 T(无论它们是什么类型),而不是 int。在你的情况下,你有一个数组其中 T = int,另一个数组其中 T = Array<int>。
建议:使用 std::size_t 代替 int 来表示大小。
英文:
The Matrix constructor taking rows and cols
Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, Array<T>(cols)) {}
tries to initialize the data member using a constructor that takes an int and an array<T>.
data is defined as
Array<Array<T>> data;
and Array does not have a constructor taking an int and an Array. You could make the constructor only taking len also take an optional T and use that to assign values to the Array:
explicit Array(int len, T copy = {}) : len{len}, buf{new T[len]} {
    std::fill_n(buf, len, copy);
}
You also need to change your Array::operator[]s to return a reference to T, not int. Your Arrays stores Ts (whatever they may be), not ints. In your case, you have one array where T = int and another where T = Array<int>.
Suggestion: Use std::size_t for the sizes instead of int.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论