大网数据广告活动
Web服务器与PHP

C2653/C2039 error when you reference STD functions

· 52CE 我爱测

Explains that when you try to reference a function from the STD C++ library header , you may receive a C2653 or a C2039 compiler error message. A workaround is provided in this article.

This article provides the information about solving the C2653 or C2039 error that occurs when you reference a function from the STD C++ library.

Original product version:   Visual C++
Original KB number:   243444

Symptoms

Attempting to reference a function from the STD C++ library header <cstdlib> using the namespace std (for example, std::exit(0)) causes the compiler to emit a C2653 or a C2039 (depending upon whether or not namespace std is defined at the point where the error is emitted) error message.

Cause

<cstdlib> does not define the namespace std. This is contrary to the Visual C++ documentation, which says:

Include the standard header <cstdlib> to effectively include the standard header <stdlib.h> within the std namespace.

Resolution

To work around the problem, place the #include <cstdlib> in the namespace std.

More information

Attempting to compile the following will cause the compiler to display the following error:

error C2653: 'std' : is not a class or namespace name

// Compile Options: /GX
#include <cstdlib>

void main()
{
    std::exit(0);
}

However, attempting to compile the following causes the compiler to display the following error:

error C2039: 'exit' : is not a member of 'std'

// Compile Options: /GX
#include <vector>
#include <cstdlib>

void main()
{
    std::exit(0);
}

In the first case, the C2653 is displayed, because the namespace std has not been defined. In the second case, the C2039 is displayed, because the namespace std has been defined (in the header <vector>), but the function exit is not part of that namespace. To work around the problem in either case, simply enclose the #include <cstdlib> in the namespace std, as follows:

// Compile Options: /GX
namespace std
{
    #include <cstdlib>
};

void main()
{
    std::exit(0);
}

Source: Microsoft Learn / IT Pro troubleshooting by Microsoft and contributors. Documentation licensed CC BY 4.0. Code samples are covered by the MIT License. Converted from Markdown, with links normalized and embedded media omitted.

资料来源与适用范围

资料来源
Microsoft Learn / IT Pro troubleshooting
适用范围
See source applicability · Source date 04/22/2020
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 4.0 (documentation), MIT (code samples)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。