Getting error when overloading << operator

Boris Borais

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

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[]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"invalid comparator" : error when overloading the "<" operator

Overloading operator[] and NOT getting "lvalue required as left operand of assignment" error

Getting SIGSEGV (segmentation fault) when overloading operator== c++

Error when erasing an object element of a vector when operator overloading is defined

Error in + operator overloading

Operator overloading giving error

Operator overloading error?

error overloading operator '>>'

Overloading operator compile error?

operator overloading error no match operator <<

"<<" Operator overloading error when defined outside main file

Error about operator overloading when I try to compile this code

C++ Operator ">>" overloading error

Error passing by value in operator overloading

Error C++ operator overloading

Error while using >> operator overloading

Overloading * operator gives no match error

How to solve operator>> overloading error (no match for 'operator>>')

Overloading a class with an object getting error?

when should i use '&' when overloading an operator?

Error when overloading operator<<, "cannot overload functions distinguished by return type alone"

MSVC error C2593 when overloading const and non-const conversion operator returning array type

operator overloading and function overloading producing ambiguous compiler error

+ operator overloading error even though it is overloaded

Strange type error with basic operator overloading

Functions returns error about overloading operator

Overloading gives me error: no match for ‘operator<’

Overloading == operator causes discards qualifiers error

overloading << operator on enums gives runtime error