Ziyad Mestour
Ziyad Mestour

Reputation: 97

Overload += for a vector class

When I overload the += it seems that it is only reaching the first element of the table. Output of the main: (20,15,15) ... normally it should be (20,20,20)

This my class vecteur:

class vecteur{
    int tab[3];
public:
    vecteur(int = 0, int =0, int = 0);
    vecteur(const vecteur&);
    ~vecteur();
    void affiche();
    friend ostream& operator << (ostream&, const vecteur&);
    friend istream& operator>> (istream&, vecteur&);
    vecteur operator+ (const vecteur&);
    vecteur& operator++ ();
    vecteur& operator++ (int);
    vecteur& operator+= (const vecteur&);
    bool operator== (const vecteur&);
    bool operator!= (const vecteur&);
};

Here's the overload of the operator +=:

vecteur& vecteur::operator+= (const vecteur& v)
{
    tab[0] += v.tab[0];
    tab[1] += v.tab[1];
    tab[2] += v.tab[2];
    return *this;
}

And the main

int main()
{
    vecteur v1(15,15,15);
    v1 += 5;
    cout << v1;
    return 0;
}

Thanks

Upvotes: 0

Views: 152

Answers (5)

Vlad from Moscow
Vlad from Moscow

Reputation: 311196

The problem is that in this statement

v1 += 5;

at first a temporary object of type vecteur is created calling constrauctor

vecteur(int = 0, int =0, int = 0);

as

vecteur( 5 );

that is the second and the third parameters have default arguments that are equal to 0.

And this temporary object with tab[0] = 5, tab[1] = 0 and tab[2] = 0 is added to v1.

You could write

v1 += { 5, 5, 5 };

instead of

v1 += 5;

and get the result you want.:)

For example

int main()
{
    vecteur v1(15,15,15);
    v1 += { 5, 5, 5 };
    cout << v1;
    return 0;
}

Upvotes: 1

user3303104
user3303104

Reputation: 11

I really don't know why your program is working since you've not overloaded the += operator to work with integers (something like: vecteur& operator+= (const int&);) and that is the operation that you are performing (v1 += 5;). But, if you try to use the += operator with an other vecteur, it should work. Example:

vecteur v1(15,15,15);
vecteur v2(5,5,5);
v1+=v2;
cout<<v1;
//This should print 20,20,20

Upvotes: 1

segfault
segfault

Reputation: 5949

What's happening in your code is that the operator overloading function takes another vector, rather than integer.

The integer 5 is converted into a vector using the constructor (int, int, int), with the first element equals to 5, second element equals to 0, and third element equals to 0.

Upvotes: 1

jrok
jrok

Reputation: 55425

The right hand side of this expression

v1 += 5;

needs to be converted vecteur (because operator+= takes a const vecteur&). That's done via implicit conversion using this constructor:

vecteur(int = 0, int =0, int = 0);

Since you only provided the first argument, the rest are defaulted to 0. And 15 + 0 gives 15 :)

Upvotes: 3

Zac Howland
Zac Howland

Reputation: 15870

Your overload does not work for the type you are supplying:

vecteur& vecteur::operator+= (const vecteur& v)
{
    tab[0] += v.tab[0];
    tab[1] += v.tab[1];
    tab[2] += v.tab[2];
    return *this;
}

This expects a vecteur parameter. v1 += 5; is passing an integer.

Since you declare a conversion constructor vecteur(int = 0, int =0, int = 0);, when you pass 5 to +=, what you are really passing is a vecteur(5, 0, 0), which is why your second and third elements are untouched.

If you want to be able to add a scalar to every element in the vecteur, you'll need another overload:

vecteur& vecteur::operator+=(int s)
{
    tab[0] += s;
    tab[1] += s;
    tab[2] += s;
    return *this;
}

I would also caution against using default parameters for constructors, to avoid these types of bugs.

Upvotes: 2

Related Questions