No match for 'operator[]'

I'm getting the following error
1
2
3
test.cpp: In function `int main()':
test.cpp:15: error: no match for 'operator[]' in 'array[i]'
test.cpp:15: error: no match for 'operator[]' in 'array[i]' 


When running this code (it's my first time using STL,, btw):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <list>

using namespace std;

int main() {
  int n;
  cin >> n;
  list<int> array[n];

  for (int i = 0; i < n; i++) {
    array[i].push_back(i);
  }

  for (list<int>::iterator i = array[i].begin(); i < array[i].end(); i++) {
    cout << *i;
  }

  return 0;
}
list<int> array[n];

This is trying to create an array of lists, which probably isn't what you want. You look like you only want a single list. Furthermore, even if you wanted an array, you can't use a non-const variable to determine the array size.

Get rid of the brackets [] everywhere, and you should be good.
Well firstly, the way you have declared array is as an array of list<int>. In each list you are adding one number, so each list only has one element. The error you are getting is because i is declared as an list<int>::iterator, and there is no operator [] for list that takes an iterator as an argument (it should be a positive integral value).
Also what Disch said, but remove the '<' operator, it should be '!='. I had certainly thought that you couldn't use a non-const variable to determine the array size, but why does the following code compile and run?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <list>

using namespace std;

int main() 
{
  int n;
  cin >> n;
  list<int> array[n];

  for (int i = 0; i < n; i++) 
  {
    array[i].push_back(i);
  }

  for (list<int>::iterator i = array[n-1].begin(); i != array[n-1].end(); i++) 
  {
    cout << *i;
  }

  return 0;
}

I had certainly thought that you couldn't use a non-const variable to determine the array size, but why does the following code compile and run?


It's legal in C. So many compilers will allow it even though it technically is not legal in C++.

Unless this changed in C++11...
@slicedpan - It's a GCC extension http://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html
>cat test.cc
void foo()
{
    int i = 7 ;
    int a[0] ; // not C++
    int b[i] ;  // not C++
}

>g++ -c test.cc

>g++ --std=c++11 -c test.cc

>g++ --std=c++11 --pedantic -c test.cc
test.cc: In function 'void foo()': 
test.cc:4:12: warning: ISO C++ forbids zero-size array 'a' [-Wpedantic]
     int a[0] ;
            ^
test.cc:5:12: warning: ISO C++ forbids variable length array 'b' [-Wvla]
     int b[i] ;
            ^

>g++ --std=c++11 --pedantic-errors -c test.cc 
test.cc: In function 'void foo()':
test.cc:4:12: error: ISO C++ forbids zero-size array 'a' [-Wpedantic]
     int a[0] ;
            ^
test.cc:5:12: error: ISO C++ forbids variable length array 'b' [-Wvla]

     int b[i] ;
            ^
The error you are getting is because i is declared as an list<int>::iterator, and there is no operator [] for list that takes an iterator as an argument (it should be a positive integral value)


Thanks, that was it.

And I actually do want an array of lists. It's for an algorithms problem where there are n blocks in n locations and can be stacked on top of each other. Seems like each location should be a list, and I'd need to store all of the lists in an array. Just in case you were wondering.
Topic archived. No new replies allowed.