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 don't really understand the error, I have tried to use a pointer to end()
, but nothing comes out. Here is the code 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’
In C/C++, an array is simply a pointer that has memory allocated by the compiler. It is not an object, and therefore has no methods.
Additionally, since it is a pointer, there is no way to know directly how big it is. Fortunately, you know it at the moment of creating it (it is
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 content of the array, so you will only see random numbers.
Here you are declaring a fixed size array. Although it
Dia
is managed as if it were a pointer, that is, you can do the following:Fixed-size arrays have certain peculiarities with respect to traditional pointers:
delete
orfree
On the other hand, as @SJuan76 tells you , pointers in C++ don't have methods ... it happens that fixed-size arrays in C++ do n't have either.
Well, although its size cannot be asked directly, this data is not essential, since you know that the range of values goes from 0 to
DIAS_SEMANA
, so the loop could look like this:Now, since you use an enumeration for the days of the week, it is not too clean to assign a value to the last element manually (which is also the value you use as the top . The normal thing is to leave the enumeration without values:
Or, if we want to generalize it even more:
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 as:
than the alternative that your code proposes:
Bonuses
Fixed-size arrays have the additional feature that the compiler knows at all times how many bytes they occupy, which allows you to calculate their size in a " simple " way:
On the other hand, though, if we want our fixed-size array to have methods, what we can do is use
std::array
(C++11 onwards):Bonuses 2
In C, if we do not put
void
in the parameters of a function, what we get is that that function accepts an indeterminate number of parameters... however in C++ thatvoid
is implicit, so if we leave the function simply as we willint main()
get the same effect than withint main(void)
but with cleaner code.The error is clear and concise, perhaps you do not understand it because it is in English; I will translate it:
The error is saying that you tried to call a member function (also known as a method) on a data type that is not-class (which is not a class); the guy you tried to call about
size
isint
.The place of the call is your error line:
If you want to have a method
size
inDia
, declare it as a collection of objects, not an array 1 :Like
std::vector
:What
std::array
Like
std::list
:If you want to know which collection best suits your needs, read this thread .
As I see in your code, you want to display the days of the week, but you use a fundamental type (an int data type ). In
wikipedia
it you can find what it means, in summary: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 keep the first error that just came out, let's continue.
using namespace std;
enum Semana
Dia[i]
is going to be all zeros. Because you have specified that itDia
is an integer that can only hold numbers inside it, and by default, C++ sets all of them to 0. So, even if you try to fix the failure of using the method.size()
, what you want to do, which is to display the days of the week, will not work for you.I hope that these clarifications help you improve your code, and thus solve your problem.