c++ - 在vs2010中使用iostream时,出现错误C2039 : 'exit' : is not a member of '` global namespace''

标签 c++ visual-studio-2010 iostream

这个问题已经困扰我好几个星期了。我使用的是 MS vs2010。

#include <iostream>
int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " << v2
    << " is " << v1 + v2 << std::endl;
    return 0;
}

C++ Primer 中的一个简单程序。当我编译它时,我得到以下错误信息:

1>e:\program files\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2039: 'exit' : is not a member of '`global namespace''

1>e:\program files\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2873: 'exit' : symbol cannot be used in a using-declaration

我试图找到一些解决方案,我得到了这个:

http://social.msdn.microsoft.com/Forums/nl-NL/Vsexpressvc/thread/31385f37-94b8-4297-b054-7fdbc5b1f51e

上面写着:

SOLUTION FOUND:

I have researched this issue on the web and it seems like it is something that has been an issue for a lot of people. The solution to this is as simple as removing a comment.

I looked through the stdlib.h file, and found the the following line was commended out:

_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);

I took out the comment and recompiled it, and now it works.

I believe in some builds the stdlib.h file will automatically be compiled with that portion of the code commented out. simple uncomment and your code will work.

显然有些人用这个解决方案解决了这个问题。然而,我什至找不到_CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code);在我的 stdlib.h 中。

有人知道如何解决这个问题吗?

最佳答案

I looked through the stdlib.h file, and found the the following line was commended out:

不应该将其注释掉。 stdlib.h 的这一部分应该如下所示:

#ifndef _CRT_TERMINATE_DEFINED
#define _CRT_TERMINATE_DEFINED
_CRTIMP __declspec(noreturn) void __cdecl exit(_In_ int _Code);
_CRTIMP __declspec(noreturn) void __cdecl _exit(_In_ int _Code);
_CRTIMP __declspec(noreturn) void __cdecl abort(void);
#endif

目前尚不清楚如何在您的文件版本中对其进行注释。但很明显,您会毫不犹豫地编辑编译器头文件来解决问题。您以前可能已经这样做过以绕过问题但不记得它。

总的来说,这是一个非常非常糟糕的主意。 Microsoft 发布了将更新编译器头文件的服务包和安全更新。但如果文件被更改,它就不会这样做。这可能会给您留下一堆令人讨厌的混合文件,这些文件不再相互兼容。

您将需要修复对这些文件造成的损坏。请注意这些文件的修改时间戳,以找出哪些文件可能已被更改。并从 friend 或同事的已知良好机器上复制这些内容。另一种可能的方法(我自己从未尝试过)是将更改的文件移至其他位置并再次运行安装程序,请求修复。实际上不确定这是否有效,它应该有效。当您这样做时,还要重新应用服务包。

关于c++ - 在vs2010中使用iostream时,出现错误C2039 : 'exit' : is not a member of '` global namespace'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014348/

相关文章:

visual-studio-2010 - 对象浏览器设置中缺少查看派生类型选项

c++ - 在 C++ 中循环遍历 .txt 文件的行

c++ - getIntegerv() 和 std::cout 的奇怪行为

visual-studio-2010 - LNK2019 : unresolved external symbol error in Visual Studio C++

visual-studio-2010 - Visual Studio 2010 调试器 - 结构调试信息仍保留旧成员名称和类型,不更新

c++ - 左值需要作为 C++ 中赋值错误的左操作数

c++ - 将 nullptr 转换为 std::optional

c++ - 从 std::cout 或 std::ofstream(file) 获取 std::ostream

c++ - 如何等待多个线程完成(使用 C++11 线程)?

c++ - 默认模板参数 - 不必来自对吗?为什么有效?