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):
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?
>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.