gpt4 book ai didi

c++ - 'localtime_s' : is not a member of 'std' in VS2015

转载 作者:行者123 更新时间:2023-11-30 03:22:37 39 4
gpt4 key购买 nike

以下 C++ 代码无法使用 VS2015 进行编译:

#include <time.h>
#include <ctime>

void main()
{
const time_t t = std::time(nullptr);

std::tm tm = {};

std::localtime_s(&tm, &time);
}

错误信息是:

Microsoft (R) C/C++ 优化编译器版本 19.00.24215.1 for x86版权所有 (C) Microsoft Corporation。保留所有权利。

t.cppt.cpp(10): error C2039: 'localtime_s': 不是 'std' 的成员C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\ctime(17):注意:参见“std”声明t.cpp(10): error C2664: 'errno_t localtime_s(tm *const ,const time_t *const )': 无法将参数 2 从 'time_t (__cdecl *)(time_t *const )' 转换为 'const time_t *const 't.cpp(10): 注意:没有上下文可以进行这种转换

有什么解决方法? (使用 std::localtime() 不是一个选项)。

最佳答案

根据 [headers]/10

Annex K of the C standard describes a large number of functions, with associated types and macros, which “promote safer, more secure programming” than many of the traditional C library functions. The names of the functions have a suffix of _­s; most of them provide the same service as the C library function with the unsuffixed name, but generally take an additional argument whose value is the size of the result array. If any C++ header is included, it is implementation-defined whether any of these names is declared in the global namespace. (None of them is declared in namespace std.)

强调我的

和表 18 将 localtime_s 列为成员,因此,由于上面的部分,我们知道它没有在命名空间 std 中定义,如果它确实存在,它将在全局命名空间中。尝试使用

::localtime_s(&tm, &time);

如果仍然不起作用,则表示您的实现不支持它。

关于c++ - 'localtime_s' : is not a member of 'std' in VS2015,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50988689/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com