The operator > is undefined for the argument - HOW TO RESOLVE ERROR (OOP)

Why is this error occuring and what can I do to solve it?

The operator > is undefined for the argument type(s) int Main.Cards

is indicated where cardX, cardY, cardH, and cardW are being called in mousePressed();
and it’s also indicated as

The operator += is undefined for the argument type(s) int Main.Cards

where cardX and cardY are being called in mouseDragged();

This is just snippets of the overall code:

//Cards array
Cards[] card = new Cards[22];

//variables from Cards class
Cards cardX;
Cards cardY;
Cards cardH;
Cards cardW;
//is the mouse on the card
boolean mouseOnCard = false;

void mousePressed() {
  //check if mouse position is within bounds of card
  if (mouseX > cardX && mouseX < cardX + cardW && mouseY > card.y && mouseY < cardY + cardH) { 
    mouseOnCard = true;
  }
}

void mouseDragged() {
  //selected card follows when dragged
  if (mouseOnCard) {
    float deltaX = mouseX - pmouseX;
    float deltaY = mouseY - pmouseY;

    cardX += deltaX;
    cardY += deltaY;
  }
}

//Class in a separate file
class Cards {
  //card size
  public int cardW = 250 / 6;
  public int cardH = 350 / 6;

  //initial location of first card
  public float cardX = 0;
  public float cardY = 0;

  //card face
  int value = 0;

  public Cards(float x, float y, int v) {
    value = v;
    cardX = x;
    cardY= y;
  }
}
1 Like

This post was flagged by the community and is temporarily hidden.

Please read the text tutorial
about objects

See Objects / Processing.org

It is obvious that from your code you don’t understand the concept of classes and objects and this is too big a topic to be covered in a single answer. Before you go any further your should look at Processing tutorial on object orientation/classes/objects also the videos on the Coding Train youtube channel.

But to get you started here is a quick guide to your problem

Cards[] card = new Cards[22];

You have declared an array of 22 cards and I assume you have created them with the new statement in your setup function.

Each card has 5 attributes / fields as defined in your class

//card size
  public int cardW = 250 / 6;
  public int cardH = 350 / 6;

  //initial location of first card
  public float cardX = 0;
  public float cardY = 0;

Now in your mousePressed function I assume that you want to find out which card the mouse is over so we will declare a global integer variable which will hold the index position of the card we want.
int overCard = -1;
If the mouse is not over the card then overCard will be set to -1 which is an invalid array index.

void mousePressed() {
    overCard  = -1; // assume mouse is not over a card
    // now check each card on turn and see if mouse position is within its bounds 
    for(int i = 0; i < card.length; i++){
        if (mouseX > card[i].cardX && mouseX < card[i].cardX + card[i].cardW && mouseY >  card[i].cardY && mouseY <  card[i].cardY +  card[i].cardH) { 
            overCard = i;
            mouseOnCard = false;
            break; // Exit for loop since we havve found a card
        }
    }
    mouseOnCard = true;
}

Now delete the following lines because they make no sense at all.

//variables from Cards class
Cards cardX;
Cards cardY;
Cards cardH;
Cards cardW;
3 Likes

If you wish you can take a look at a very old deck of cards implementation I did a long time ago:

It’s split into 2 sketches:
1 creates & saves the “.csv” file, while the other loads it and makes a deck of cards out of it.

For convenience I’ve merged them now into 1 sketch w/ 4 “.pde” tabs:

“Table_CSV_Card_Pack.pde”:

/** 
 * Table CSV Card Pack (v1.0.1)
 * by GoToLoop (2022/Sep/13)
 *
 * https://discourse.Processing.org/t/
 * the-operator-is-undefined-for-the-argument-type-s-int-
 * how-to-resolve-error/38705/6
 *
 * https://forum.Processing.org/two/discussion/2801/
 * picking-cards-at-random-then-excluding-those-from-further-picking-#Item_2
 */

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

static final String ARCHIVE = "Cardpack.csv", EXT = ".jpg";
static final int NUM = 5, GAP = 040, FPS = 60, FILL = 0200;

final List<Card> myHand = new ArrayList<Card>(NUM);
Card[] cards;

void setup() {
  size(600, 900);
  frameRate(FPS);
  noLoop();

  fill(FILL);
  textSize(GAP);
  textAlign(CENTER);

  cards = loadCards(ARCHIVE, EXT, true, false);
  //cards = loadCards(ARCHIVE, EXT, true, true);

  showDeckContent(true, cards);
  println(ENTER);

  mousePressed();
}

void draw() {
  clear();

  for (int i = myHand.size(); i-- != 0; 
    myHand.get(i).displayAt(width >> 1, i*GAP + GAP));
}

void mousePressed() {
  final int btn = mouseButton;
  final int len = myHand.size();

  if (btn == RIGHT & len > 0)  myHand.remove(len - 1);
  else if (btn == LEFT)        pickCard();
  else                         PickNewRandomHand(NUM);

  println(myHand);
  println();

  redraw();
}

“Card_Class.pde”

class Card implements Comparable<Card> {
  short x, y;

  final byte w, h;
  final byte value, group;
  final int hash;

  final String rank, suit, info;
  final PImage img;

  Card(final int val, final String r, final String s, final PImage pic) {
    value = (byte) abs(val);
    rank = r;
    suit = s;
    img = pic;

    group = (byte) suit.charAt(0);
    hash = group<<020 | value;

    w = (byte) (img == null? 0 : img.width);
    h = (byte) (img == null? 0 : img.height);

    info = rank + " of " + suit;
  }

  Card setPos(final int xx, final int yy) {
    x = (short) xx;
    y = (short) yy;

    return this;
  }

  Card display() {
    return displayAt(x, y);
  }

  Card displayAt(final int x, final int y) {
    if (img != null)  set(x, y, img);
    else              text(info, x, y);

    return this;
  }

  String toString() {
    return info;
  }

  int hashCode() {
    return hash;
  }

  boolean equals(final Object o) {
    return o instanceof Card? ((Card) o).hash == hash : false;
  }

  int compareTo(final Card o) {
    return value != o.value? 
      Integer.signum(value - o.value) :
      Integer.signum(group - o.group);
  }
}

“Card_Functions.pde”:

Card[] loadCards(
  final String name, final String ext, 
  final boolean gotHeader, final boolean toLoadImages)
{
  final Table table;

  if (!dataFile(name).isFile())  table = createAndSaveTableFile(name);
  else {
    table = loadTable(name, gotHeader? "header" : "");
    table.setColumnType(0, Table.INT);
  }

  final Card[] deck = new Card[table.getRowCount()];

  for (int i = 0; i != deck.length; ++i) {
    final TableRow row = table.getRow(i);

    final int num = row.getInt(0);
    final String rank = row.getString(1), suit = row.getString(2);
    final PImage img = toLoadImages? loadImage(row.getString(3) + ext) : null;

    deck[i] = new Card(num, rank, suit, img);
  }

  return deck;
}

static final void showDeckContent(final boolean toSort, final Card... cards) {
  println();
  println(cards);
  println();

  if (toSort) {
    Arrays.sort(cards);
    println(cards);
    println();
  }

  for (final Card c : cards) {
    final int  rank = c.hash & 0xFF;
    final char suit = (char) (c.hash >> 020);
    print(" - " + rank + suit);
  }
}

void pickCard() {
  if (myHand.size() >= cards.length)  return;

  Card c;

  do c = cards[(int) random(cards.length)];
  while (myHand.contains(c));

  myHand.add(c);
  Collections.sort(myHand);
}

void PickNewRandomHand(int qty) {
  if ((qty = min(abs(qty), cards.length))
    == (cards.length | myHand.size()))  return;

  myHand.clear();

  if (qty == cards.length) {
    myHand.addAll(Arrays.asList(cards));
    return;
  }

  while (qty != 0) {
    final Card c = cards[(int) random(cards.length)];

    if (!myHand.contains(c)) {
      myHand.add(c);
      --qty;
    }
  }

  Collections.sort(myHand);
}

“Table_CSV_File_Creation.pde”:

static final String[] RANKS = {
  "Ace", "Two", "Three", "Four", "Five", 
  "Six", "Seven", "Eight", "Nine", "Ten", 
  "Jack", "Queen", "King"
};

static final String[] SUITS = {
  "Clubs", "Diamonds", "Hearts", "Spades"
};

Table createAndSaveTableFile(final String filename) {
  final Table table = new Table();

  table.addColumn("Number", Table.INT);
  table.addColumn("Rank");
  table.addColumn("Suit");
  table.addColumn("Image");

  for (int r = 0, s = 0; s != SUITS.length; ++s, r = 0)
    while (r != RANKS.length) {
      final TableRow newRow = table.addRow();

      newRow.setString("Rank", RANKS[r]);
      newRow.setString("Suit", SUITS[s]);

      newRow.setInt("Number", ++r);

      newRow.setString("Image", str(r) + 
        Character.toLowerCase(SUITS[s].charAt(0)));
    }

  saveTable(table, dataPath(filename));
  return table;
}
1 Like

here is my version,
based on your code


//Cards array
Card[] cards = new Card[22];

//is the mouse on the card
boolean mouseOnCard = false;
int overCard=-1; 

// --------------------------------------------------------------------

void setup() {
  size(1400, 400);

  background(0); 
  for (int i=0; i<cards.length; i++) { 
    cards[i]=new Card( 30 + i * (250 / 6 + 7), 
      30, 
      i );
  }
}

void draw() {
  background(0); 
  for (Card currentCard : cards) {
    currentCard.display();
  }
}

// -------------------------------------------------------------------------------------------------------

void mousePressed() {
  overCard  = -1; // assume mouse is not over a card

  // now check each card on turn and see if mouse position is within its bounds
  int i=0; 
  for (Card currentCard : cards) {
    if (currentCard.mouseInside()) {
      // start dragging 
      overCard = i;
      mouseOnCard = true;
      return; // Exit FUNC since we havve found a card
    }//if
    i++;
  }//for
  mouseOnCard = false;
}//func 

void mouseDragged() {
  //selected card follows when dragged

  if (overCard==-1) 
    return;  // Exit FUNC 

  if (mouseOnCard) {
    float deltaX = mouseX - pmouseX;
    float deltaY = mouseY - pmouseY;

    cards[overCard].cardX += deltaX;
    cards[overCard].cardY += deltaY;
  }
}

void mouseReleased() {
  // end dragging
  overCard    = -1; // release card 
  mouseOnCard = false;
}

///===================================================

//Class in a separate file
class Card {

  //card size
  int cardW = 250 / 6;
  int cardH = 350 / 6;

  //initial location of card
  float cardX = 0;
  float cardY = 0;

  //card face
  int value = 0;

  //constr --------------
  Card(float x, float y, 
    int v) {

    cardX = x;
    cardY = y;

    value = v;
  }//constr --------------

  void display() {
    fill(255); 
    rect(cardX, cardY, 
      cardW, cardH);

    fill(0); 
    text(value, 
      cardX+18, cardY+17);
  }

  boolean mouseInside() {
    return
      mouseX > cardX &&
      mouseX < cardX + cardW && 
      mouseY > cardY &&
      mouseY < cardY + cardH;
  }//method
  //
}//class
//

3 Likes