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.