r/learnprogramming • • 16d ago

Solved Expecting primary expression error

I keep getting the error error: expected primary expression before 'mouse' and error: expected primary expression before 'rect'

Can someone assist me on this because I am lost

CONTEXT

I made a boolean function that detects if a point is colliding with a rectangle and in my main function when button is pressed and mouse is colliding with rectangle than the console needs to output string

bool pointRectCollision(Mouse mouse, RectangleShape rect){
      if(mouse.getPosition().x > int(rect.getPosition().x) && mouse.getPosition.x() < int(rect.getPosition().x + rect.getSize().x) && mouse.getPosition().y > int(rect.getPosition().y) && mouse.getPosition.y() < int(rect.getPosition().y + rect.getSize().y)){
           return true;
      }

      else {
          return false;
       }

}

int main(){

  // GAME LOOP
  if (mouse.isButtonPressed(Button::Left) && pointRectCollision(mouse, pauseBtn)){
        cout << "COLLISION" << endl;
    }


  return 0;
}

UPDATE

I was able to fix the problem. I had to fix the syntax of how I was declaring mouse and also nest my conditional statement for detecting collision.

// function to detect point-rectangle collision
bool pointRectCollision(Vector2i mousePos, RectangleShape &rect){ 
  if(mousePos.x > int(rect.getPosition().x) && mousePos.x < int(rect.getPosition().x + rect.getSize().x) && mousePos.y > int(rect.getPosition().y) && mousePos.y < int(rect.getPosition().y + rect.getSize().y)){
      return true;
}

  else {
    return false;
  }

}

int main(){
  // GAME LOOP

  // MOUSE
  if (Mouse::isButtonPressed(Mouse::Button::Left)){
    if(pointRectCollision(Mouse::getPosition(window), pauseBtn)){
      cout << "collision" << endl;
    }

   }

}
4 Upvotes

8 comments sorted by

View all comments

2

u/rupertavery64 16d ago

Is Mouse and RectangleShape declared in a header or before the method call?