2

这个问题已经困扰我好几个星期了。我正在使用 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

其中说:

找到的解决方案:

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.

Apparently some people fixed the problem with this solution. However, I could not even find _CRTIMP __declspec(noreturn) void __cdecl exit(__in int _Code); in my stdlib.h.

Anyone knows how to fix this?

4

1 回答 1

3

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

It should NOT be commented out. That part of stdlib.h should look like this:

#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

It isn't clear how it got to be commented in your version of the file. But it is clear that you don't hesitate to edit compiler header files to get out of a problem. You may have done this before to bypass a problem and not remember it.

In general, this is a Really Really Bad Idea. Microsoft releases service packs and security updates that will update compiler header files. But it will not do so if the file was altered. Which may leave you with a nasty mixed bag of files that are no longer compatible with each other.

You will need to fix the damage done to these files. Pay attention to the modified timestamp of these files to find out which might have been changed. And copy those from a known-good machine of, say, a friend or colleague. Another possible approach (never tried it myself) is to move the altered files somewhere else and run setup again, asking for a repair. Not actually sure if that works, it should. Also re-apply service packs when you do it that way.

于 2012-12-23T21:05:04.137 回答