Search code examples
c++stdvisual-studio-2022

Trying to upgrade / compile old C++ source (from a vendor) - problem with "std::runtime_error"


I'm trying to compile some sample code from a product we use. I don't think the vendor has maintained the code. I'm using VS Pro 2022 Ver 17.7.5 Here's the snipped of code and the errors it generates. I haven't used C++ or Visual Studio for 15 years. Everything is way more complicated and obscure that I recall. I'm trying to fix the "runtime_error" problem on line #17. I can't understand the documentation for that. I've tried shotgunning it by putting parens around stuff in line 17 to no avail.

/* ReadDynaRecordListWithWhere.cpp : Defines the entry point for the console application.
    The purpose of this example is to show how to define a dynamic query and read only
    requested column data from the TeamTrack database. This example shows how to request
    information about the users who are members of the "Everyone" group in TeamTrack.
*/

#include "stdafx.h"
#include <stdio.h>
#include <new.h>
#include "TSServer.h"
#include "TSDynaRecordList.h"

// Create these before we start because when a memory allocation
// fails it will bee too late (no memory to create new stuff).
static const char * memoryMessage = "Memory allocation failed.";
#ifdef WIN32
static const std::runtime_error bad_alloc( memoryMessage );  // *** Line #17

// Define a function to be called if new fails to allocate memory.
int TSNewHandler( size_t size )
{
  printf( "Throwing: %s\n", memoryMessage );
  throw bad_alloc;                                           // *** Line #23
  return NULL;
}
#endif

Error list:

Severity  Code   Description                                                                    Line
Error     C2039  'runtime_error': is not a member of 'std'                                      17    
Message          see declaration of 'std'                                                       25    
Error     C4430  missing type specifier - int assumed. Note: C++ does not support default-int   17    
Error     C2146  syntax error: missing ';' before identifier 'bad_alloc'                        17    
Error     C4430  missing type specifier - int assumed. Note: C++ does not support default-int   17    
Error     C2040  'memoryMessage': 'int' differs in levels of indirection from 'const char *'    17    
Error     C2065  'bad_alloc': undeclared identifier                                             23    

Solution


  • Edit 2023-11-05:

    I added "#include <stdexcept>" as per @PeteBecker, @PepijnKramer and @Jarod42. That eliminated the errors. Many thanks to the responders!

    Although the "new" didn't seem to be causing problems, I made the changes as outlined by @MarekR. Those changes did not induce any new problems so they will remain inplace.