'mem_fun' : is not a member of 'std

2019-08-11 00:15发布

问题:

I've been experiencing this error for a while now, on every project that I have built with the CryEngine2 SDK in Visual Studio 2013 Professional. Most of the time, I have just edited the function from this:

void CMultipleGrabHandler::Reset()
{
    std::for_each (m_handlers.begin(), m_handlers.end(), std::mem_fun (&CBaseGrabHandler::Reset));

    std::vector <CAnimatedGrabHandler*>::iterator it = m_handlers.begin();
    std::vector <CAnimatedGrabHandler*>::iterator end = m_handlers.end();
    for ( ; it != end; ++it)
        delete *it;

    m_handlers.erase (m_handlers.begin (), m_handlers.end ());
}

to this:

void CMultipleGrabHandler::Reset()
{
}

I know it's not a good approach to the problem, but it got rid of the 'mem_fun' : is not a member of 'std' error.

I am now looking for a solution to overcome this problem, since I have just started working on a new project, where stability is key; it's not good if I remove the body of CMultipleGrabHandler::Reset(), since it could possibly induce crashes in certain situations. I have browsed the internet for a solution to this problem, but I have not found anything (closest thing I found was an explanation of what mem_fun does).

I have tried taking std:: off mem_fun, but I just get an mem_fun is undefined error, suggesting that this isn't exactly the right way to go about mending this error.

The project was originally created in Visual Studio 2005, and was migrated to Visual Studio 2013's format when I first opened the solution. Might this be the cause of the problem? How can it be fixed?

Edit: Added visual-studio-2015 as this also applies to the new VS version.

回答1:

The function std::mem_fun is defined in the header <functional>.

On some older compilers, you don't necessarily need to #include a standard library header in order to use functions and classes defined in that header, because it might already be included by some other standard library header that you #include. This is not exclusive to VS; it was also true for older gcc versions, like 4.0. Newer compilers are more standard-conforming and will require you to #include the actual headers that define standard library functions and classes.