lundi 18 juin 2018

Overloading operator += with array c++

I am doing a c++ program. Here's what I have to do: I create an array of the size I want. The array is auto-filled with 0.

With the operator += i have to insert 1 at the place i chose. Example :

array += 2; 
will insert 1 at the index 2 of my array.

But how can I do it ?

My .h file

#ifndef BITARRAY_H
#define BITARRAY_H
#include <ostream>

class bitArray
{
public:
    bitArray(int n);
    virtual ~bitArray();

    bitArray& operator+=(const bitArray&); //this operator
    bitArray& operator-=(const bitArray&);
    int& operator[] (int x) {
      return sortie[x];
  }
protected:
private:
    int sortie[];
    int n;
};

//ostream& operator<<(ostream&, const bitArray&);
#endif // BITARRAY_H

My method in the cpp file :

bitArray& bitArray::operator+=(const bitArray& i)
{
    this ->sortie[i] = 1;
    return *this;
}

But it does not work. Am I doing the right way?

My error is :

no match for 'operator[]' (operand types are 'int [0]' and 'const bitArray')|

Thank you in advance !

Aucun commentaire:

Enregistrer un commentaire