Error compiling "request for member 'size' in '* (int *) (& Dia)"

2

I have implemented a code for an enum that contains the days of the week, the problem that I get is with the function size() , I do not understand much the error, I tried to use a pointer to end() , but not I get nothing. Here is the code that I have made:

#include<iostream>
using namespace std;

int main(void){
    enum Semana{
        Lunes=1, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo, DIAS_SEMANA=7};

    int Dia[DIAS_SEMANA];



    for(int i=0; i<(Dia.size()); i++){//La linea del error
        cout<<Dia[i]<<endl;

    }
}

The error message when compiling:

 error: request for member ‘size’ in ‘*(int*)(& Dia)’, which is of non-class type ‘int’
    
asked by AER 13.10.2018 в 15:55
source

4 answers

3
int Dia[DIAS_SEMANA];

Here you are declaring an array of fixed size. Although Dia is managed as if it were a pointer, that is, you can do the following:

int* ptr = Dia;

Fixed-size arrays have certain peculiarities with respect to the pointers of a lifetime:

  • Are stored in the program's stack
  • Its size can not be changed at run time
  • Your memory can not be released with delete or free

On the other hand, as you say @ SJuan76 , the C ++ pointers do not have methods ... it happens that in C ++ the arrays of fixed size, do not either.

Well, although you can not ask its size directly, this data is not essential, since you know that the range of values goes from 0 to DIAS_SEMANA , then the loop could look like this:

for(int i=0; i<DIAS_SEMANA; i++)

Now, since you use a list for the days of the week, it is not too clean to assign a value to the last item manually (which is also the value you use for top . The normal thing is to leave the enumerated one without values:

enum Semana{Lunes, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo, DIAS_SEMANA};

Or, if we want to generalize it even more:

enum Semana{Lunes, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo, INICIO = Lunes, DIAS_SEMANA};

Why?

Well basically because if not the iterations are a bit weird. Any C ++ programmer will find it more natural to have a loop such that:

for( Semana i = INICIO; i < DIAS_SEMANA; i++ )
  Dia[i] = /* ... */;

that the alternative that your code raises:

for( Semana i = Lunes; i <= DIAS_SEMANA; i++ )
  Dia[i-1] = /* ... */;
  //  ^ OJO que en tu codigo tienes que Lunes=1

Bonus

The fixed-size arrays have as an additional feature that the compiler knows at all times how many bytes they occupy, which allows you to calculate its size in a " simple ":

int numElementos = sizeof(Dia)/sizeof(Dia[0]);

Although, on the other hand, if we want our fixed-size array to have methods, what we can do is use std::array (C ++ 11 onwards):

#include <array>
#include <iostream>

using namespace std;

int main(){

    enum Semana{ Lunes, Martes, Miercoles, Jueves, Viernes, Sabado, Domingo, DIAS_SEMANA};

    std::array<int,DIAS_SEMANA> Dia;

    // Iteracion por indices
    for(size_t i=0; i<Dia.size(); i++){
        cout<<Dia[i]<<endl;
    }

    // Iteracion por iteradores
    for(std::array<int,DIAS_SEMANA>::iterator it = Dia.begin(); it != Dia.end(); ++it){
        cout<<*it<<endl;
    }

    // Iteracion por iteradores (C++11)
    for(auto it = Dia.begin(); it != Dia.end(); ++it){
        cout<<*it<<endl;
    }

    // for basado en rangos
    for(int item : Dia){
        cout<<item<<endl;
    }
}

Bonus 2

In C, if we do not put void in the parameters of a function, what we get is that this function accepts an indeterminate number of parameters ... however in C ++ that void is implicit, so if we leave the function simply as int main() we will get the same effect as with int main(void) but with a cleaner code.

    
answered by 14.10.2018 / 02:45
source
4

In C / C ++, an array is simply a pointer assigned to the memory by the compiler. It is not an object, and therefore it has no methods.

Additionally, since it is a pointer, there is no way to know directly the size it has. Fortunately, you know it when you create it (it's DIAS_SEMANA ).

In any case, and once the question is solved, it is not clear what you are trying to do, because you have not initialized the contents of the array, so you will only see numbers at random.

    
answered by 13.10.2018 в 16:05
3

The error is clear and concise, maybe you do not understand it because it is in English; I will translate it:

error: request for member ‘size’ in ‘*(int*)(& Dia)’, which is of non-class type ‘int’
error: petición del miembro ‘size’ en ‘*(int*)(& Dia)’, que es del tipo no-clase ‘int’

The error goes to saying that you tried to call a member function (also known as a method) on a data type that is non-class (which is not a class); the type you tried to call size is int .

The place of the call is your line of error:

for(int i=0; i<(Dia.size()); i++){//La linea del error
//              ~~~ <--- Dia es no-clase

If you want to have a method size in Dia , declaring it as a collection of objects, not as a training 1 :

As std::vector :

std::vector<int> Dia(DIAS_SEMANA);

As std::array

std::array<int, DIAS_SEMANA> Dia;

As std::list :

std::list<int> Dia(DIAS_SEMANA);

If you want to know which collection best suits your needs, read this thread .

  • Also known as array or in English array.
  • answered by 15.10.2018 в 08:37
    1

    As I see in your code, you want to show the days of the week, but you use a fundamental type (a data type int ). In the wikipedia you can find what it means, in summary:

      

    Depending on the machine and the compiler used, primitive types may occupy a certain size in memory. The following list illustrates the number of bits that different primitive types occupy in the x86 architecture.

         

    Other architectures may require different sizes of primitive data types. C ++ does not say anything about the number of bits in a byte, or the size of these types [...]

    But in C ++, these fundamental data or primitive data are not classes. This means that there is no such function that you call Dia.size() .

    But let's not just stay with the first mistake that just came out, let's move on.

    • The use of using namespace std; is not recommended
    • The enumerators are a type of data. It's better for you, in the long run, to leave them out of any scope (outside the keys). In this way, you will be able to know from any function what is enum Semana
    • Try to take the screen ... what? The days of the week in text format? Because what you are going to get out of Dia[i] will be all zeros. Because you have specified that Dia is an integer that can only hold numbers inside it, and by default, C ++ puts all of them to 0. So, even if you try to fix the failure to use the .size() method, it will not work for you what you want to do, which is to screen the days of the week.

    I hope these clarifications help you improve your code, and thus, solve your problem.

        
    answered by 16.10.2018 в 15:17