Home > front end >  Getting error when overloading << operator
Getting error when overloading << operator

Time:08-24

I have a Class called "Vector". It consists of two private fields: std::vector<double> coordinates and int len. Methoddim() returns len.

I am overloading operator << like that:

friend std::ostream& operator<<(std::ostream& os,  Vector& vec ) 
{
    std:: cout << "(";
    for ( int i = 0; i < vec.dim(); i   ) {
        if ( i != vec.dim()-1){
            os << vec[i] << ", ";
        } else {
            os << vec[i];
        }
    }
    os << ')';
    return os;
}

An operator like that:

friend Vector operator  (Vector& first, Vector& second)
{
    if(first.dim() != second.dim()){
        throw std::length_error{"Vectors must be the same size"};
    }else {
        Vector localVec(first.dim()); // same as {0,0,0...,0} - first.dim() times 
        for (int i = 0; i < first.dim(); i  ){
            localVec[i] = first[i]   second[i];
        }
        return localVec;
    }
}

And operator [] like that:

double& operator[](int index)
{
    return this->coordinates[index];
}

And here's the problem:

Vector x{1,2,4};
Vector y{1,2,3};
    
Vector z = x   y; 
std:: cout << z; // it works perfectly fine - (2, 4, 7)

std:: cout << x   y; // it gives me an error 
  • could not match 'unique_ptr<type-parameter-0-2, type-parameter-0-3>' against 'Vector' operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)

It seems to me that this error is related to parameter Vector& vec , but I don't know whether it's right and what should I do to fix it. If anyone could give me a hint (or tell me what I should read more about) - I would be very grateful.

Here's full code:

 class Vector
{
    private:
        std::vector <double> coordinates;
        int len;
    public:
        Vector(): len{0}, coordinates{} {};
        Vector(std::initializer_list <double> coordinates_): len{static_cast <int>( coordinates_.size())}, coordinates{coordinates_} {}
        Vector(int len) : len{len} {
            for(int i = 0; i < len; i  ){
                coordinates.push_back(0);
            }
        }
        int dim() const
        {
            return this->len;
        }

        double& operator[](int index)
        {
            return this->coordinates[index];
        }

        friend std::ostream& operator<<(std::ostream& os,  Vector& vec ) 
        {
            std:: cout << "(";
            for ( int i = 0; i < vec.dim(); i   ) {
                if ( i != vec.dim()-1){
                    os << vec[i] << ", ";
                } else {
                    os << vec[i];
                }
            }
            os << ')';
            return os;
        }

        friend Vector operator  (Vector& first, Vector& second)
        {
            if(first.dim() != second.dim()){
                throw std::length_error{"Vectors must be the same size"};
            }else {
                Vector localVec(first.dim());
                for (int i = 0; i < first.dim(); i  ){
                    localVec[i] = first[i]   second[i];
                }
                return localVec;
            }
        }
};

CodePudding user response:

friend std::ostream& operator<<(std::ostream& os, Vector& vec)

This signature doesn't accept rvalues, and this is why the error happens for your temporary result here:

std:: cout << x y;

Change the second parameter into const Vector& vec or provide an overload with an r-value reference parameter.

friend std::ostream& operator<<(std::ostream& os, const Vector& vec);

CodePudding user response:

A temporary cannot bind to a non-const reference argument. You are missing const in at least two places. Most importantly here:

friend std::ostream& operator<<(std::ostream& os, const Vector& vec ) 
                                                // ^^ 

And there should a const overload of operator[]

  • Related