Question

I'm getting a compiler error:

error C2061: syntax error : identifier 'bad_alloc'

I've used the chunk of code below in other projects before, with different memory allocation within the try block, without problems. I was hoping someone could explain to me why bad_alloc isn't being recognized by VS10 despite it not causing the same problem in use in other programs? Odds are I missed some minor syntactical thing, but I've spent hours trying to figure it out, and at this point I feel like I'm probably blind to the error. Thanks for the help!

try
{
    node* tbr = new node();
    return tbr;
} // End try allocation

catch(bad_alloc)
{
     throw OutOfMemoryException();
} // End catch(bad_alloc)
Was it helpful?

Solution

bad_alloc is defined in the header new.

#include <new>

In namespace std.

using namespace std
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top