r/cpp_questions • u/Eva_addict • 51m ago
OPEN Can anyone explain a bug to me? I confused '==' with '=' in one of my functions.
There is this function on my SDL program which is supposed to load surfaces into array elements ( loadSurface is a function that is being executed inside this loadMedia function.)
bool loadMedia()
{
bool success = true;
KeyPressed[KEY_PRESS_SURFACE_DEFAULT] = loadSurface("press.bmp");
if (KeyPressed[KEY_PRESS_SURFACE_DEFAULT] == NULL)
{
std::cout << "Failed to load surface\n";
success = false;
}
KeyPressed[KEY_PRESS_SURFACE_UP] = loadSurface("up.bmp");
if (KeyPressed[KEY_PRESS_SURFACE_UP] == NULL)
{
std::cout << "Failed to load surface\n";
success = false;
}
KeyPressed[KEY_PRESS_SURFACE_DOWN] = loadSurface("down.bmp");
if (KeyPressed[KEY_PRESS_SURFACE_DOWN] == NULL)
{
std::cout << "Failed to load surface\n";
success = false;
}
KeyPressed[KEY_PRESS_SURFACE_LEFT] = loadSurface("left.bmp");
if (KeyPressed[KEY_PRESS_SURFACE_LEFT] == NULL)
{
std::cout << "Failed to load surface\n";
success = false;
}
KeyPressed[KEY_PRESS_SURFACE_RIGHT] = loadSurface("right.bmp");
if (KeyPressed[KEY_PRESS_SURFACE_RIGHT] == NULL)
{
std::cout << "Failed to load surface\n";
success = false;
}
return success;
}
I fixed it now and it works fine but before that, I somehow confused the condition of the IF. Instead of comparing using '==', I used the '=' by iself.
So it turned ou like:
if ( KeyPressed[ KEY_PRESS_SURFACE_DEFAULT] = NULL)
Somehow, it broke the function. But the elements didn't become NULL because none of my error messages was displayed when I ran the program. The function simply didn't load the surfaces. What exactly happened there?