微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从类型为“MyVector<int>”的右值对“MyVector<int>&”类型的非常量引用进行无效初始化

如何解决从类型为“MyVector<int>”的右值对“MyVector<int>&”类型的非常量引用进行无效初始化

我知道这方面有很多线程,但经过几次之后,我仍然没有解决我的问题。

我有一个自定义向量类,我正在尝试创建一个函数,该函数接受一个向量并将其添加到当前向量中,然后返回它。

下面是我的向量类的代码

MyVector.h


template<class type>
class MyVector {
    private:
        type* m_data;
        size_t m_capacity = 1;
        size_t m_size = 0;

    public:
        MyVector() {
            this->m_size = 0;  // this->m_size is 0 because there are no elements
            this->m_capacity = 1;  // this->m_capacity starts off 1 more than this->size
            this->m_data = new type[this->m_capacity];  // Allocate the memory required
        }

        MyVector(MyVector<type>& other) {
            this->m_capacity = other.m_capacity;  // The capacity will be the same
            this->m_size = other.size();  // The size will also be the same
            this->m_data = new type[this->m_capacity];  // Allocate the memory required to hold the data

            /* copy all the data from the vector to this object */
            for (size_t i = 0; i < other.size(); i++) {
                this->m_data[i] = other[i];
            }
        }
        MyVector(std::initializer_list<type> init_list) {
            int init_list_size = init_list.size();  // This will be the size of the initializer list: { 1,2,3,... }
            this->m_capacity = init_list_size + 1;  // Make the capacity one more to make it work when trying to append
            this->m_data = new type[this->m_capacity];  // Allocate the memory required

            /* copy all the data */
            for (type t : init_list) {
                this->m_data[this->m_size++] = t;
            }
        }

        MyVector<type>& operator=(MyVector<type> other) {
            this->m_capacity = other.m_capacity;  // The capacity will be the same as the other vector
            this->m_size = other.m_size;  // The size will be the same as the other vector
            this->m_data = new type[this->m_capacity];  // Allocate the memory required

            /* copy the data from the other vector to this */
            for (size_t i = 0; i < other.size(); i++) {
                this->append(other.get(i));
            }
            return *this;
        }
        /* Functionality to append,delete,etc... */

        type get(size_t index) const {
            return this->m_data[index];
        }

        /* PROBLEMATIC CODE */
        MyVector<type> add(MyVector<type>& other) {
            MyVector<type> result;
            for (size_t i = 0; i < this->size(); i++) {
                result.append( this->get(i) + other.get(i) );
            }
            return result;
        }
        
};

以下是我如何实现 MyVector 类。

ma​​in.cpp

#include "../include/MyVector.h"

int main() {
    MyVector<int> a = { 1,4,5 };
    MyVector<int> b = a;
    MyVector<int> c = a.add(b);  // error: invalid initialization of non-const reference of type 'MyVector<int>&' from an rvalue of type 'MyVector<int>'

    return 0;
}

我得到的错误错误:从'MyVector'类型的右值对'MyVector&'类型的非常量引用进行无效初始化

连同注意:初始化'MyVector::MyVector(MyVector&) [with type = int]'的参数1

我不明白为什么不能将等式的左侧分配给右值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。