C++ 错误“系统”含糊不清?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23900978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error "system" is ambiguous?
提问by 0000
I have a simple program, and it works fine, but the system("CLS");
and system("pause");
statements have red IntelliSense lines underneath them. When I move my cursor over them it says Error "system" is ambiguous.
What is causing that?
我有一个简单的程序,它运行良好,但是system("CLS");
andsystem("pause");
语句下面有红色的 IntelliSense 行。当我将光标移到它们上面时,它会说Error "system" is ambiguous.
是什么原因造成的?
Here is my code:
这是我的代码:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int choice = 0;
const double PI = 3.14159;
double sideSquare = 0.0;
double radius = 0.0;
double base = 0.0;
double height = 0.0;
cout << "This program calculates areas of 3 different objects." << endl;
cout << "1.) Square" << endl;
cout << "2.) Circle" << endl;
cout << "3.) Right Triangle" << endl;
cout << "4.) Terminate Program" << endl << endl;
cout << "Please [Enter] your object of choice: ";
cin >> choice;
system("CLS"); // The problem is here...
switch(choice)
{
case 1:
cout << "Please [Enter] the length of the side of the square: ";
cin >> sideSquare;
cout << "The area is: " << pow(sideSquare, 2) << endl;
break;
case 2:
cout << "Please [Enter] the radius of the circle: ";
cin >> radius;
cout << "The area is: " << PI * pow(radius, 2) << endl;
break;
case 3:
cout << "Please [Enter] the base of the triangle: ";
cin >> base;
cout << endl << "Now [Enter] the height of the triangle: ";
cin >> height;
cout << "The area is: " << (base * height) / 2 << endl;
break;
default:
cout << "Please [Enter] a valid selection next time." << endl;
return 0;
}
system("pause"); // ... and here.
return 0;
}
回答by Mooing Duck
You need to #include <cstdlib>
你需要 #include <cstdlib>
Source: http://en.cppreference.com/w/cpp/utility/program/system
来源:http: //en.cppreference.com/w/cpp/utility/program/system
Also, try to avoid system
, it's dangerous. To pause the program when it's finished, put a breakpoint on the }
at the end of main. There isn't a standard way to clear the screen unfortunately.
另外,尽量避免system
,这是危险的。要在程序完成时暂停程序,请}
在 main 的末尾放置一个断点。不幸的是,没有标准的方法来清除屏幕。
For future reference, the red squiggles are intellisenseerrors, which are shown by a different front-end than the one that actually compiles the code, so the red squiggles are sometimes wrong, especially with complex templates. In most cases, including this one, it's correct though.
为了将来参考,红色波浪线是智能感知错误,由与实际编译代码的前端不同的前端显示,因此红色波浪线有时是错误的,尤其是对于复杂模板。在大多数情况下,包括这个,它是正确的。