错误"系统"暧昧吗?

时间:2014-05-28 00:17:08

标签: c++ visual-studio-2010 intellisense

我有一个简单的程序,它工作正常,但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;
}

1 个答案:

答案 0 :(得分:10)

您需要#include <cstdlib>

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

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

为了将来参考,红色波浪线是 intellisense 错误,它们由与实际编译代码的前端不同的前端显示,因此红色波浪线有时是错误的,尤其是复杂的模板。在大多数情况下,包括这个,它是正确的。