0

I know that there are many threads on this, but after going through several, I still haven't solved my problem.

I have a custom vector class and I am trying to create a function that takes in a vector and adds it to the current vector, then returns it.

Below is the code for my vector class.

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;
        }
        
};

The following is how I implemented the MyVector class.

main.cpp

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

int main() {
    MyVector<int> a = { 1, 2, 3, 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;
}

The error I get is error: invalid initialization of non-const reference of type 'MyVector&' from an rvalue of type 'MyVector'.

Along with note: initializing argument 1 of 'MyVector::MyVector(MyVector&) [with type = int]'

What I don't understand is why the left-side of the equation cannot be assigned to an rvalue.

Aayaan Sahu
  • 125
  • 1
  • 8
  • 2
    `MyVector(MyVector& other)` the copy constructor should not be capable of altering the instance being copied. Change to `MyVector(const MyVector& other)` and two problems, one being the one you're struggling with, go away. – user4581301 Jun 04 '21 at 16:48
  • 2
    What went wrong: `MyVector add(MyVector& other)` returns a temporary `MyVector`. You cannot take a non-constant reference to a temporary because A) altering a temporary is pointless as it's out of scope and gone almost immediately and B) storing the reference is pointless for the same reason. – user4581301 Jun 04 '21 at 16:52
  • 1
    Also, your assignment operator leaks memory, as it does not free the existing `m_data` array before allocating a new array for it. Since the operator takes `other` *by value* and `MyVector` implements copy construction, all of the hard work has already been done by the constructor, so the operator should simply take ownership of the copied array that `other`'s constructor has prepared (see the [copy-swap idiom](https://stackoverflow.com/questions/3279543/)). – Remy Lebeau Jun 04 '21 at 17:13

0 Answers0