简体   繁体   中英

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 !

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

The error is crystal clear that the operator[] expecting an interger type and what you passing a bitArray class type. Simple fix is to change it to integer.

However, here:

private:
    int sortie[];
    int n;

it is highly recommended to use std::vector , which gives a contiguous dynamic array whereas sortie[] is static allocation. Something like this:

See live here

#include <iostream>
#include <vector>
#include <cstddef>

class bitArray
{
private:
   std::vector<int> sortie;
public:
   explicit bitArray(int size): sortie(size) {}
   bitArray& operator+=(const std::size_t i)
   {
      if (0 <= i && i < sortie.size()) // check for (0 <= index < size) of the array
      {
            this ->sortie[i] = 1;
            return *this;
      }
      else
      {
         // do your logic! for instance, I have done something like follows:
         std::cout << "out of bound" << std::endl;
         if(sortie.size() == 0) sortie.resize(1,0); // if the size of array == 0            
      }
      return *this;
   }
   int operator[] (const std::size_t index)
   {
      return (0 <= index && index < sortie.size()) ? sortie[index] : -1;
   }
};
int main ()
{
   bitArray obj(3);
   obj += 0;  std::cout << obj[0] << std::endl;
   obj += -2; std::cout << obj[-2] << std::endl;
   obj += 22; std::cout << obj[22] << std::endl; 
   return 0;
}

Update : Using C++17 feature std::optional , modified the above solution with the optional return type, which should be more readable.

See output in wandbox

#include <iostream>
#include <vector>
#include <cstddef>
#include <optional>

class bitArray
{
private:
   std::vector<int> sortie;
public:
   explicit bitArray(int size): sortie(size) {}
   // optional is used as the return type
   std::optional<bitArray> operator+=(const std::size_t i)
   {
      if (i < sortie.size()) // check for (0 <= index < size) of the array
      {
            this -> sortie[i] = 1;
            return std::optional<bitArray>{*this};
      }
      std::cout << "out of bound operation+= \t";
      return std::nullopt;    // std::nullopt to create any (empty) std::optional
   }
   std::optional<int> operator[] (const std::size_t index)
   {
      if(index < sortie.size())   return std::optional<int>{sortie[index]};
      else
      {
         std::cout << "out of bound operator[]: ";
         return std::nullopt;
      }
   }
};
int main ()
{
   bitArray obj(3);
   obj += 0;  std::cout << obj[0].value_or(-1) << std::endl;
   obj += -2; std::cout << obj[-2].value_or(-1) << std::endl;
   bitArray obj1(0);
   obj1 += 22; std::cout << obj1[22].value_or(-1) << std::endl;
   return 0;
}

Your operator+= takes a bitArray as parameter, but it should take the index where to set the 1 and thats basically what the error message is trying to tell you: There is no overload for the parameters you are trying to use it.

Note that to get such an operator you dont need to write your own array class, but you can provide an overload for std::vector :

#include <iostream>
#include <vector>

template <typename T> 
std::vector<T>& operator+=(std::vector<T>& v,size_t index) {
    v[index] = 1;
    return v;
}     

int main() {
    auto vec = std::vector<int>(10,0);
    vec += 5;
    std::cout << vec[5];
    return 0;
}

Note that this is a rather uncommon way to implement += (it does not really add anything). I would consider this as misuse of operator overloading and it will lead to obfuscated code. Consider this:

vec[5] = 1;

vs

vec += 5;

In the first line, everybody who is familiar with std::vector will know what it does, while for the second line 90% of the expectations will be off. I guess you are doing this as part of an assignment or homework, though for anything else I would suggest you to stay away from using operator overloads that do anything more than the obvious thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM