2

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 !

2
  • 4
    The argument should be an int. Commented Jun 18, 2018 at 10:44
  • 1
    @rustyx Or size_t. By the way your array sortie seemingly needs to by dynamically allocated, since n is a runtime parameter. Commented Jun 18, 2018 at 10:46

2 Answers 2

2

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;
}
Sign up to request clarification or add additional context in comments.

9 Comments

If you're going to check for out of bounds and are passing an int, you should probably check that the value is at least 0 as well.
@Qubit : you mean this: if (i < n && i > 0)?
...or pass a size_t which is anyway more natural to use for an index than an int
Why passing i by const reference? It's not a good practice, see, e.g., Passing integers as constant references versus copying.
If you are using a vector, n is superfluous
|
1

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.

3 Comments

@David consider accepting one of the answers. I would actually vote for the other one, as it addresses your problem more directly
@user463035818 uncommon way to implement += that's a good point, which I missed. +1
@JeJo when I was new to C++ i loved the fact that you can make operators do anything you want to. However, I became much more conservative and if it isnt crystal clear and obvious what the operator does, i'd always prefer a method with a descriptive name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.