错误"系统"是不明确的?

000*_*000 1 c++ intellisense visual-studio-2010

我有一个简单的程序,它工作正常,但system("CLS");system("pause");语句下面有红色的IntelliSense线.当我将光标移到它们上面时,它会说Error "system" is ambiguous.是什么原因造成的?

这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

Moo*_*uck 10

你需要 #include <cstdlib>

资料来源:http://en.cppreference.com/w/cpp/utility/program/system

另外,尽量避免system,这很危险.要在程序结束时暂停程序,请}在main的末尾放置一个断点.遗憾的是,没有标准的方法来清除屏幕.

为了将来参考,红色波浪线是智能感知错误,它们由与实际编译代码的前端不同的前端显示,因此红色波形有时是错误的,特别是对于复杂的模板.在大多数情况下,包括这个,但它是正确的.

  • 还可以尝试`std :: system`; C++实现通常将C库函数注入全局命名空间以及`std`,因此可以想象IDE认为`:: system`和`std :: system`是不同的 (3认同)
  • 或者,只要您使用`/ SUBSYSTEM:CONSOLE`链接器选项,如果您"无需调试就开始"(CTRL + F5),控制台程序将在Visual Studio中终止时自动暂停.请参阅[此答案](http://stackoverflow.com/a/1152873/1227469)和[可能这一个](http://stackoverflow.com/a/23288778/1227469).让程序本身暂停通常是错误的解决方案,如果您想要做的就是在终止后查看输出,因为释放的控制台程序通常在打开的控制台内终止,因此不需要暂停. (2认同)