I fixed the problem, it was necessary to put this code in the event loop. As a result, I have this:
Event event;
while(window.pollEvent(event))
{
if (event.type == Event::Closed)
{
window.close();
}
if (event.type == Event::MouseButtonPressed)
{
if (event.mouseButton.button == Mouse::Left)
{
//Some code
}
}
}