-4

I am trying to loop through the array and get the elements inside in C++. Here is my code:

int result;
int index_array [] = {11,12,13,14,15,16,17,18,19,20};

for (int count =0; count < index_array.length() ; count++){
  if(count%2 == 0){
    cout << "Elements at the even index are " << index_array[count] << endl;
  }
}

If I change the for loop to:

for (int count =0; count < 10 ; count++){

There is no error because my array only consists of 10 items. But if I used the .length() method, there is an error which is expression must have a class type. I have no idea what is it, as in if it is in Eclipse, there contains a more detailed error description. What might be wrong?

Updated answer

    for (int count =0; count < sizeof(index_array)/sizeof(index_array [0]) ; count++){
    if((count+1)%2 == 0){
        cout << "Elements at the even index are " << index_array[count] << endl;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Rauryn
  • 177
  • 2
  • 4
  • 16
  • 4
    Arrays are not "Objects" per say in C++, and have therefore no associated method. The error "expression must have class type" expresses just that : you can only call methods on classes (or structures). – Nbr44 Apr 27 '13 at 11:08
  • 2
    If you're sure you want a fixed length array, you can change `index_array.length()` to `sizeof( index_array ) / sizeof( index_array[0] )` – Dave Apr 27 '13 at 11:08
  • Sorry? What do you mean? Can you give me some example? – Rauryn Apr 27 '13 at 11:09
  • @Rauryn: http://stackoverflow.com/questions/37538/how-do-i-determine-the-size-of-my-array-in-c – John Zwinck Apr 27 '13 at 11:10
  • @Rauryn you've come from a language where everything is an object. In the lower-level languages like C++, that's not true. The array is just a space in memory. It doesn't have any methods, or even know how big it is. The compiler does (because in this case it's fixed-length), so `sizeof` can tell you. – Dave Apr 27 '13 at 11:10
  • Oh okay okay. I thought C++ was kind of similar with java. So no matter is vector or array, they only can use sizeof method? – Rauryn Apr 27 '13 at 11:14
  • `sizeof` is not a method. its an operator implemented by each implementation. you can think of primitve types as a bunch of memory locations given a special name. – Koushik Shetty Apr 27 '13 at 11:18
  • Err, not everything is an object, even in Java. Specifically, it has `int`, `double` and so on. Python, on the other hand, ... – paxdiablo Apr 27 '13 at 11:30

4 Answers4

6

You can't call length() on int index_array[], it is a primitive array, not an object.

You could call size(), if you have, for example vector<int> index_array.

Adam Stelmaszczyk
  • 19,665
  • 4
  • 70
  • 110
  • So only vector works with .length method? And array can only use the sizeof method? – Rauryn Apr 27 '13 at 11:12
  • [Vector](http://www.cplusplus.com/reference/vector/vector/) has `size()` method which does what you want. Yes, primitive arrays have no methods. All you can do is call sizeof *operator* on them. – Adam Stelmaszczyk Apr 27 '13 at 11:14
2

There is not .length for a plain array in C++. Instead use std::vector and you can use method size() :

std::vector<int> index_array {11,12,13,14,15,16,17,18,19,20};

for (int count =0; count < index_array.size() ; count++){
    if(count%2 == 0){
        cout << "Elements at the even index are " << index_array[count] << endl;
    }
}

Also in your case, you can calculate the length of the array:

int length = sizeof(index_array)/sizeof(index_array[0]);
masoud
  • 55,379
  • 16
  • 141
  • 208
  • I wrote the vector like this : vector index_array {11,12,13,14,15,16,17,18,19,20}; But there's an error at the first curly braces there. What's wrong? – Rauryn Apr 27 '13 at 11:21
  • Ah, I wrote the code in `-std=c++11` mode. you can change the compiler mode to C++11 to have initializer lists. – masoud Apr 27 '13 at 11:28
1
int index_array [] = {11,12,13,14,15,16,17,18,19,20};

This is not an object that you can invoke some length() method on. Instead, it's a regular array, just like in C.

You can do one of two things.

The first is to use one of the C++ collection classes such as std::vector (adjustable size) or std::array (constant size) with their size() methods:

// C++11 syntax
std::vector<int> index_array {11,12,13,14,15,16,17,18,19,20};

// Pre C++11 syntax
int ia_src[] = {11,12,13,14,15,16,17,18,19,20};
vector<int> index_array (ia_src, ia_src + sizeof (ia_src) / sizeof (*ia_src));

std::array<int,10> index_array = {11,12,13,14,15,16,17,18,19,20};

The second is to simply treat the array as an array, in which case the length of that array can be found with the expression:

sizeof (index_array) / sizeof (*index_array)

Just be aware that this only works for arrays. If you pass that array to a function, it will decay to a pointer to the first element and sizeof will no longer work as you expect. You need to get the size while it's still an array and pass that along with it.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Arrays in c++ are not object (classes) so they don't have neither methods nor attributes. May be you can use the Array class instead and get the size like std::array::size()

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60