C++ error: invalid initialization of reference of type ‘Array<int>&’ from expression of type ‘int’

huangapple go评论107阅读模式
英文:

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&lt;typename T&gt;
class Matrix {
public:
    Matrix(): rows(0), cols(0), data(){}

    Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, Array&lt;T&gt;(cols)) {}

    Array&lt;T&gt;&amp; operator[](int row) {
        return data[row];
    }
    // other functions

private:
    int rows, cols;
    Array&lt;Array&lt;T&gt;&gt; data;
};


Array:

template&lt;typename T&gt;
class Array {
public:
    Array(): len{0}, buf{nullptr} {
    }

    explicit Array(int len) : len{len}, buf{new T[len]} {
    }
    // copy constructor, move constructor, destructor
    int&amp; operator[](int index) {
        if (!in_bounds(index))
            throw std::string(&quot;Exception operator[](&quot; + std::to_string(index) + &quot;) Out Of Range&quot;);
        return buf[index];
    }
    const int&amp; operator[](int index) const {
        if (!in_bounds(index))
            throw std::string(&quot;Exception operator[](&quot; + std::to_string(index) + &quot;) Out Of Range&quot;);
        return buf[index];
    }
private:
    int len; 
    T* buf;
    bool in_bounds(int index) const {
        return index &gt;= 0 &amp;&amp; index &lt; 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 构造函数使用 rowscols 参数来初始化 data 成员,使用一个接受 intarray&lt;T&gt; 的构造函数。

data 被定义为

Array&lt;Array&lt;T&gt;&gt; data;

Array 没有接受 intArray 的构造函数。你可以让只接受 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&lt;int&gt;

演示

建议:使用 std::size_t 代替 int 来表示大小。

英文:

The Matrix constructor taking rows and cols

Matrix(int rows, int cols) : rows(rows), cols(cols), data(rows, Array&lt;T&gt;(cols)) {}

tries to initialize the data member using a constructor that takes an int and an array&lt;T&gt;.

data is defined as

Array&lt;Array&lt;T&gt;&gt; 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&lt;int&gt;.

Demo

Suggestion: Use std::size_t for the sizes instead of int.

huangapple
  • 本文由 发表于 2023年5月25日 14:04:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定