user2957672
user2957672

Reputation: 25

cannot convert from Card to int

So I have this array of the type Card, lets call it CardDeck[] . I need to save the info from CardDeck[0] to use it later. I thought I could do the following:

Card memory = theCards[0];
    for(int i=0;i<51;i++){

            theCards[i]=theCards[i+1];
            theCards[51]=theCards[memory];

To me, what is happening is that first I create a new variable called memory of the "type" Card, and the info in theCards[0] gets copied to it.

Then every "card" gets moved one step to the left, for example Card that are in the 2nd spot gets moved (or copied rather) to the first slot, the 3rd Card gets moved to the 2nd slot, and so on, all the way until the 52nd card gets moved to the 51nd slot. Then the card which originally was in the first slot, which we saved to the memory moves into the 52nd slot.

However the last line doesnt work, it says cannot convert from Card to int and I dont get it, I dont use an int type, its all Card types so whats up?

Upvotes: 0

Views: 207

Answers (1)

MAV
MAV

Reputation: 7457

Try this instead:

Card memory = theCards[0];
for(int i=0;i<51;i++){
   theCards[i]=theCards[i+1];
}
theCards[51]= memory;

You're are trying to use memory as an index into the array. An index is always an integer, whereas memory is an instance of Card.

When you write Card memory = theCards[0]; you are saying that you want the card stored at position 0 in theCards and you want to assign it to a variable named memory.

If you were to say Card memory = theCards[42]; it would be the Card stored at position 42 you want to assign to memory.

When you say theCards[51]=theCards[memory]; you are trying to say that you want the Card at position some card and you want to place it in your array at position 51. But "position some card" does not make sense, memory is not a position, it is an actual Card. It is like saying to the grocer that you want ice cream number Sun Lolly. You don't want ice cream number Sun Lolly, you want the Sun Lolly.

Upvotes: 1

Related Questions