提问人:mnemotronic 提问时间:11/5/2023 最后编辑:mnemotronic 更新时间:11/6/2023 访问量:106
尝试升级/编译旧的C++源(来自供应商) - “std::runtime_error”问题
Trying to upgrade / compile old C++ source (from a vendor) - problem with "std::runtime_error"
问:
我正在尝试从我们使用的产品中编译一些示例代码。我不认为供应商维护了代码。我正在使用 VS Pro 2022 版本 17.7.5 下面是代码的截图及其生成的错误。我已经 15 年没有使用 C++ 或 Visual Studio 了。我记得,一切都更加复杂和晦涩。 我正在尝试解决第 #17 行的“runtime_error”问题。我无法理解这方面的文档。我试过通过在第 17 行的东西周围放 parens 来霰弹枪,但无济于事。
/* 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
错误列表:
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
答:
-1赞
mnemotronic
11/6/2023
#1
编辑 2023-11-05:
我按照@PeteBecker、@PepijnKramer和@Jarod42添加了“”。这消除了错误。非常感谢响应者!#include <stdexcept>
尽管“新”似乎没有引起问题,但我还是按照@MarekR概述进行了更改。这些变化没有引起任何新的问题,因此它们将继续存在。
我(仍然)收到很多关于“警告MSB8012”的警告,但会在 StackO 的其他地方尝试这些建议。
评论
0赞
pmacfarlane
11/7/2023
请使用问题中的编辑链接添加其他信息。“发布答案”按钮应仅用于完整回答问题。- 从评论
评论
<new.h>
#include <stdexcept>
#include
<stdexcept>
#include <new>
#include <stdexcept>
throw bad_alloc;
throw std::bad_alloc();
throw std::bad_alloc{};
std::
std::bad_alloc