Swap Function Won't Accept Correct Parameters

I’m trying to use a simple swap function, but for some reason it won’t accept the array of custom objects as a parameter even though that’s what I specified when making the function. I made sure the array I put in is the correct type, but it isn’t working. I’m sure the fix is simple, perhaps I missed something when I set up the “Card” class, but I’ve tried searching it on Google and nothing helpful comes up. There wasn’t much code to cut out since I just started this, so I hope I didn’t leave too much in.

The problem is on line 29, the error message is
The function swap() expects parameters like: " swap(Card[], int, int)"

class Card {
  
  boolean faceUp = true;
  
  void flip() {
    this.faceUp = !this.faceUp;
  };
  
}

Card[] cards = new Card[5];

void setup() {
  size( 256, 512 );
  background(0);
  
  for ( int i = 0; i < 5; i++ ) {
    cards[i] = new Card();
  }
}

void doFlip( int amt ) {
  if ( amt % 2 == 0 ) {    
    for ( int i = 0; i < amt / 2; i++ ) {
      swap( cards, cards[i], cards[amt - i] );
    }
  }
}

void swap( Card[] array, int a, int b ) {
  Card temp = array[a];
  array[a] = array[b];
  array[b] = temp; 
}
1 Like

Hi OrangeC7,

When you do cards[i] you are getting a Card, not an integer.

So your swap function should be void swap(Card[] array, card a, card b).

That’s why you are getting your error :wink:

EDIT

Actually, after reading your swap function you should change the call to your function, not the function itself.

Replace swap(cards, cards[i], cards[amt - i] ); by swap(cards, i, amt - i);

4 Likes

Ah, thanks! I knew the fix would be as simple as that. :smiley: