Search code examples
c++operator-overloadingcoutostream

When overloading the << operator from std::ostream, why does the compiler give a "too many parameters for this operator function" error?


I have a struct that represents a 2d column vector. I've overloaded some operators such as * to mean scalar multiplication in the context of an int and + to mean vector addition in the context of another vector.

I also want to overload the << operator so that I can simply pass the object to cout and have the two elements printed. Currently I am overloading it like below;

struct vector2d
{
private:
    float x;
    float y;
public:
    vector2d(float x, float y) : x(x), y(x) {}
    vector2d() : x(0), y(0) {}


    vector2d operator+(const vector2d& other)
    {
        this->x = this->x + other.x;
        this->y = this->y + other.y;
        return *this;
    }

    vector2d operator*(const int& c)
    {
        this->x = this->x*c;
        this->y = this->y*c;
        return *this;
    }

    friend std::ostream& operator<<(std::ostream& out, const vector2d& other)
    {
        out << other.x << " : " << other.y;
        return out;
    }
};

This works fine, but if I remove the friend keyword I get "too many parameters for this operator function". What does this mean and why does the friend keyword fix it?


Solution

  • With the friend, the operator is not a member of the class, so it requires 2 input parameters.

    Without the friend, the operator becomes a member of the class, and thus requires only 1 parameter, so you would need to remove the vector2d parameter and use the hidden this parameter instead.