-4

I am learning C++ on the fly and am having problems with vectors so I am writing some programs that use vectors to familiarize myself with them.

I was following the advice from this post regarding printing out the value of a vector's size() call:

How can I get the size of an std::vector as an int?

My code is a simple C++ code:

#include <vector>

int main(int argc, char ** argv) {
   /* create an int vector of size 10, initialized to 0 */
   std::vector<int> int_list[10];
   int int_list_size;

   int_list_size = static_cast<int>(int_list.size()); // <-- compilation error here
} // End main()

I'm on Ubuntu 16.04 and I get this error:

"error: request for member 'size' in 'int_list', which is of non-class type 'std::vector<int> [10]'

Since the size of the vector int_list is 10, shouldn't size() return 10, which I can then cast to an int?

SQA777
  • 352
  • 5
  • 15
  • The size of the vectors are each 0. The size of the array of vectors is 10. And C-style arrays do not have a `size()` method. – Eljay Jul 08 '19 at 22:17
  • 5
    "I am learning C++ on the fly" - get a good book. –  Jul 08 '19 at 22:18
  • What do you thing the type of `int_list` is? What does the compiler think it is? (If you are using a IDE, try hovering the cursor over the symbol to get some info about it...) – dmckee --- ex-moderator kitten Jul 08 '19 at 22:19
  • 2
    You are trying to call `size()` on an *array* of vectors, not on a *particular* vector. A C style array does not have any methods. A C++ style `std::array` does, though – Remy Lebeau Jul 08 '19 at 22:20
  • Neil Butterworth, I'm working through the book "C++ Primer". It had some examples of codes and I was working through some of the examples – SQA777 Jul 08 '19 at 22:35
  • I guarantee it didn't have any code like `int_list.size()` –  Jul 08 '19 at 22:37

1 Answers1

6

You are not creating a vector, you are creating an array of vector:

std::vector<int> int_list[10];

You should use:

std::vector<int> int_list(10);

See:

NoDataFound
  • 11,381
  • 33
  • 59
  • 6
    The `, 0` is redundant and cplusplus.com is a rubbish site (prefer cppreference.com) – M.M Jul 08 '19 at 22:20
  • OK, now I see the problem. I misread "()" as "[]". – SQA777 Jul 08 '19 at 22:40
  • *array of vectors –  Jul 08 '19 at 23:38
  • @NoDataFound, I found the error. I misread "()" as "[]". I'm doing some experimental programming and a related question came up. If I declare an array of vectors: std::vector int_list[10]; this means there's an array and each element in the array is a vector. Is there a way to initialize each of the vectors to be of size 5? std::vector int_list[10](5) // this doesn't work. Could I do this without creating a vector of vectors of ints? – SQA777 Jul 09 '19 at 22:18
  • @M.M: Fixed the 0. While you may think that cplusplus is rubbish, cppreference is kind of harder to read (not the content, but how it is styled). – NoDataFound Jul 10 '19 at 08:41
  • 1
    @SQA777: I don't do enough C++ nowadays to help you with this one. I presume you ought to declare it: `int_list[10] {std::vector(5), std::vector(5), ...}` or use a loop. – NoDataFound Jul 10 '19 at 09:30