0

I programmed in C for a while and now I'm new to C++. I have to replace the for-loops in a C-Programm with the range-based for loops of C++11. This is the actual code:

 void sort1(int num[], int start, int end) {
  int finished = 0, i;

  while(!finished) {
    finished = 1;
    for(i=start ; i<end ; i++) {
      if(num[i] > num[i+1]) {
        swap(num, i, i+1);
        finished = 0;
      }
    }
  }
}

And this is what I changed it to:

void sort1(int num[], int start, int end) {
  bool finished = 0;

  while(!finished) {
    finished = 1;
    for(int i : num) {
      if(i > i+1) {
        swap(num, i, i+1);
        finished = 0;
      }
    }
  }
}

I cannot even compile it, it gives me the error: error: no matching function for call to 'begin(int*&)'

(Sorry if my english is a bit weird :D)

SimonH
  • 1,385
  • 15
  • 35
  • `num` decays to `int *`, so you cannot call `std::begin` on it. –  Oct 09 '14 at 17:46
  • 1
    @remyabel: `num` does not decay to a pointer, it is a pointer. (There might have been decay in the calling function, but that's a different thing.) – Deduplicator Oct 09 '14 at 17:54
  • I would change the type of num[] to std::array instead of a plain array which decays to a pointer when handed over to a function like this. You can then pass the array by reference and your function should work as it should. – Brosch Mar 11 '21 at 13:04

0 Answers0