I HAVE AN ERROR

I hope you can help me.
I have that error:
error: incompatible types in assigment of 'int' to 'int[365]'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const int MAX_HOTELS=5;
const int MAX_DAYS=365;
typedef int TableRooms[MAX_DAYS];
struct Hotel{
    string name;
    double price;
    int numberRoom;
    TableRooms nroomempty;
};
typedef Hotel TableH[MAX_HOTELS];

struct ComplexHotel{
    TableH t;
    int n;
};

void initilize(ComplexHotel h){
    for (int i=0;i<365;i++){
        h.t[i].nroomempty=0; // HERE I HAVE THE ERROR

    }
}


All the program works perfectly instead at the action initizalize where I have that error: error: incompatible types in assigment of 'int' to 'int[365]'
You are trying to initialize TableRooms (or an int[365]) to the value of 0. There is a much easier way to do this, and this is to have a constructor within the struct that initializes it for you:
1
2
3
4
5
6
7
8
9
10
11
//...
struct Hotel {
    Hotel() : name(), price(0.0), numberRoom(0) {
        for (size_t i = 0; i < MAX_DAYS; ++i)
            nroomempty[i] = 0;
    }
    string name;
    double pirce;
    int numberRoom;
    TableRooms nroomempty;
};


Then, when you create an object of Hotel, it will automatically initialize the values to zero, removing the need for a function call.
Thank you for answering but I can't use constructors and objects because the teachers haven't explained yet.

There is some way to do it without using a constructor?


Thank you :)
nroomempty is an array of int, 0 is a int, so when you write h.t[i].nroomempty=0; that causes an error.

There are also other problems : you pass the object by value and your loop boundaries are wrong

I think the correct code should be:
1
2
3
4
5
6
7
void initilize(ComplexHotel* h){
    for (int j=0;j<MAX_HOTELS;j++){
        for (int i=0;i< MAX_DAYS;i++){
            h->t[j].nroomempty[i]=0;
        }
    }
}

Last edited on
First: Like toum said - don't pass-by-value. When you do that, a copy of the variable is created. That copy is deleted when the function ends, so "initilize" actually does nothing. If you want the object to be changed, sent it as a pointer, but notice that the syntax changes as a result! Like toum said, now it's "h->t[j]" and not "h.t[j]".(that's just in case you didn't cover pointers yet..) You can avoid that by passing h as reference:
void initilize(ComplexHotel &h) {}
With the "&" prefix, you can now treat it like a normal variable, and changing it will actually change the original variable.

Second: "nroomempty" is an array. An array is a pointer to the first variable in the sequence. Setting it as 0 will ruin the pointer and leak memory. In your case its impossible because you have't dynamically allocated the array (if you don't know what that means, just gloss over that.)
If what you want to do is set each individual variable in the array to 0, the way to do that, like toum pointed out, is to make a loop which goes over all the array:
1
2
3
4
5
for (int j=0;j<MAX_HOTELS;j++){
        for (int i=0;i< MAX_DAYS;i++){
            h->t[j].nroomempty[i]=0;
        }
    }

Same way you made a loop which goes over all the "TableH" array.
Hope it helps :)_
Last edited on
Topic archived. No new replies allowed.