-7

I'm new to C++, can anyone explain what's meant by this?:

no match for ‘operator[]’ (operand types are ‘std::initializer_list’ and ‘int’)

In this program:

#include <algorithm>
#include <array>
#include <iostream>

using namespace std;

void
print_array(const auto& range)
{
cout << endl;
for (auto& element : range)
    cout << element << ' ';
cout << endl;
}

void
sort(auto& range)
{
for (int i = 0; i < range.size() - 1; i++) {
    swap(range[i], min_element(range[i + 1], range[range.size() - 1]));
}
}

int
main()
{
auto arr = { 1, 4, 2, 0, 9, 11 };

print_array(arr);
sort(arr);
print_array(arr);

return 0;
}

Compilation:

 g++ main.cpp -Ofast -std=gnu++17 
main.cpp: In instantiation of ‘void sort(auto:2&) [with auto:2 = std::initializer_list<int>]’:
main.cpp:30:11:   required from here
main.cpp:20:15: error: no match for ‘operator[]’ (operand types are ‘std::initializer_list<int>’ and ‘int’)
    swap(range[i], min_element(range[i + 1], range[range.size() - 1]));
        ~~~~~^
main.cpp:20:37: error: no match for ‘operator[]’ (operand types are ‘std::initializer_list<int>’ and ‘int’)
    swap(range[i], min_element(range[i + 1], range[range.size() - 1]));
                                ~~~~~^
main.cpp:20:51: error: no match for ‘operator[]’ (operand types are ‘std::initializer_list<int>’ and ‘std::initializer_list<int>::size_type {aka long unsigned int}’)
    swap(range[i], min_element(range[i + 1], range[range.size() - 1]));

Since I said I was new to C++, explaining/demonstrating how ‘std::initializer_list works or giving a relevant link or demonstrating how I could access range[index] properly in this case would be much more helpful than getting triggered and downvoting.

Thank you for reading.

coder13
  • 47
  • 1
  • 6
  • The operands are `std::initializer_list` and `int`, not an array and `int` Try look it up – Passer By Aug 05 '17 at 00:13
  • As a side-note you can't use the `auto` specifier in parameter declaration like that. Start by reading one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Ron Aug 05 '17 at 00:14
  • Possible duplicate of [Is there a way to pass auto as an argument in C++?](https://stackoverflow.com/questions/29944985/is-there-a-way-to-pass-auto-as-an-argument-in-c) – user4581301 Aug 05 '17 at 00:15

1 Answers1

-1

I've never used array before, so hopefully someone will correct me if I've done something dumb.... So something about C++ is you always need to understand what type of data you are working with, unless you are just passing it along. So, 1) let me strongly recommend you not use auto for the time being. 2) array is....kind of a problem for a beginner. It's sort of like throwing all the C nonsense and all the C++ nonsense at you at the same time. Try C-style arrays or list for the time being? list has a sort function.

#include <algorithm>
#include <array>
#include <iostream>

using namespace std;

void
print_array(const array<int, 6>& range)
{
  cout << endl;
  for (auto& element : range)
    cout << element << ' ';
  cout << endl;
}

void
randomlySwap(array<int, 6>& range)
{
  //does not sort..exercise for reader
  for (int i = 0; i < range.size() - 1; i++)
  {
    swap(range[i], range[i+1]);
  }
}

int
main()
{
  array<int, 6> arr = { 1, 4, 2, 0, 9, 11 };

  print_array(arr);
  randomlySwap(arr);
  print_array(arr);
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
zzxyz
  • 2,953
  • 1
  • 16
  • 31