Win32 errors with Algorithm functions min() and max()

Apr 27, 2012 at 6:00pm
Hello all! I have been learning how to use the win32 api from a book, but I've encountered an error that I can not seem to fix. Before I go any further, here is the functions giving me errors, along with all of my include files
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// UFO C++ Source

// Include Files
#include "Ufo.h"
#include <algorithm>

void GameCycle()
{
    // Update the saucer position
    _iSaucerX = min(500 - _pSaucer->GetWidth(), max(0, _iSaucerX + _iSpeedX));
    _iSaucerY = std::min(320, std::max(0, _iSaucerY + _iSpeedY));

    // Force a repaint to redraw the saucer
    InvalidateRect(_pGame->GetWindow(), NULL, FALSE);
}

void HandleKeys()
{
    // Change the speed of the saucer in responce to arrow key presses
    if(GetAsyncKeyState(VK_LEFT) < 0)
        _iSpeedX = max(-_iMAXSPEED, _iSpeedX - 2);
    else if (GetAsyncKeyState(VK_RIGHT) < 0)
        _iSpeedX = min(_iMAXSPEED, _iSpeedX + 2);
    else
        _iSpeedX *= .95;

    if (GetAsyncKeyState(VK_UP) < 0)
        _iSpeedY = max(-_iMAXSPEED, _iSpeedY - 2);
    else if (GetAsyncKeyState(VK_DOWN) < 0)
        _iSpeedY = min(_iMAXSPEED, _iSpeedY + 2);
    else
        _iSpeedY = min(_iMAXSPEED, _iSpeedY += .5);
}


And here is the header file for that, which may help:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

// Include Files
#include <windows.h>
//#include <cstring>
#include <algorithm>
#include "Resource.h"
#include "GameEngine.h"
#include "Bitmap.h"

// Global Variables
HINSTANCE    _hInstance;
GameEngine*  _pGame;
const int    _iMAXSPEED = 8;
Bitmap*      _pBackground;
Bitmap*      _pSaucer;
double       _iSaucerX, _iSaucerY, _iSpeedX, _iSpeedY;
//char       _cDebug[256]; 


Now, when this code was copied verbatim from the book, I had no error. However, when I added some friction (_iSpeedX *= .95;) and gravity (_iSpeedY = min(_iMAXSPEED, _iSpeedY += .5);), all of a sudden every single instance of min() and max gives the error "error: 'max' was not declared in this scope". I think that this is related to the namespace, as putting std:: in front of the operations sometimes helps. I have done some researching, yet I can't seem to find a related instance. Does anybody know what the problem could be?
Last edited on Apr 27, 2012 at 6:02pm
Apr 27, 2012 at 6:10pm
max and min is in the std namespace so it should work if you write std::max, std::min.
Apr 27, 2012 at 6:15pm
That only works for the first two min()'s
Apr 27, 2012 at 6:20pm
If I do that for all of them, I get the error
error: no matching function for call to 'max(int, double'
, which is strange. The functions are included in the #include <algorithm> header. I might just have to make my own min() and max() functions...
Apr 27, 2012 at 6:22pm
Make sure the two arguments to min and max are of the same type.
Apr 27, 2012 at 6:31pm
They are
Apr 27, 2012 at 6:35pm
std::min(_iMAXSPEED, _iSpeedX + 2);
Here the fist argument is type int and the second argument is type double. I guess you want to use the double version of this function so you have to cast the first argument to a double.
std::min(static_cast<double>(_iMAXSPEED), _iSpeedX + 2);
Apr 27, 2012 at 6:38pm
Well, that did it! Thank you, I hadn't thought of that.
Topic archived. No new replies allowed.