C++ 系统未在范围内声明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3885901/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:58:10  来源:igfitidea点击:

System not declared in scope?

c++compiler-errors

提问by Nilo Cortex

I know its simple code, How do I fix "System not declared in scope" problem?

我知道它的简单代码,如何解决“系统未在范围内声明”的问题?

#include<iostream>
using namespace std;

int main(void)
{
    system ( "TITLE Calculator" );
    system ( "COLOR 2" );
    char cChar;
    double dfirstnumber;
    double dsecondnumber;
    char cDoagain;

    do
    {
        system("CLS");
        cout << "Please enter the first number you would like to use."<< endl;
        cin >> dfirstnumber;
        cout<< "Please enter the operation you would like to perform." << " (+,-,*,or /)" << endl;
        cin >> cChar;
        cout<< "Please enter the second number you would like to use." << endl;
        cin >> dsecondnumber;

        switch (cChar)
        {
            case '+':
                cout << "The answer is: " << dfirstnumber << "+" << dsecondnumber << "=" <<
                (dfirstnumber + dsecondnumber) << endl;
                break;
            case '-':
                cout << "The answer is: " << dfirstnumber << "-" << dsecondnumber << "=" <<
                (dfirstnumber - dsecondnumber) << endl;
                break;
            case '*':
                cout << "The answer is: " << dfirstnumber << "*" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'x':
                cout << "The answer is: " << dfirstnumber << "x" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case 'X':
                cout << "The answer is: " << dfirstnumber << "X" << dsecondnumber << "=" <<
                (dfirstnumber * dsecondnumber) << endl;
                break;
            case '/':
                if(dsecondnumber == 0){
                cout<< "That is an invalid operation." << endl;}
                else{
                cout << "The answer is: " << dfirstnumber << "/" << dsecondnumber << "=" <<
                (dfirstnumber / dsecondnumber) << endl;

        }
                break;
                default:
                    cout << "That is an invalid operation." << endl;
                    break;
    }
                cout << "Would you like to start again? (Y/N)" << endl;
                cin >>  cDoagain;
    }while (cDoagain == 'Y' or cDoagain == 'y');
    system("PAUSE");
    return 0;
}

Heres my end message:

这是我的结束信息:

C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp||In function 'int main()':| C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp|8|error: 'system' was not declared in this scope||

|=== Build finished: 1 errors, 0 warnings ===|

C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp||在函数'int main()'中:| C:\Documents and Settings\Nilo\My Documents\Work\Testing\main.cpp|8|错误:'system' 未在此范围内声明||

|=== 构建完成:1 个错误,0 个警告 ===|

回答by Paul R

You need to add:

您需要添加:

 #include <cstdlib>

in order for the compiler to see the prototype for system().

为了让编译器看到system().

回答by Timo Geusch

Chances are that you've not included the header file that declares system().

可能是您没有包含声明system().

In order to be able to compile C++ code that uses functions which you don't (manually) declare yourself, you have to pull in the declarations. These declarations are normally stored in so-called header files that you pull into the current translation unit using the #includepreprocessor directive. As the code does not #includethe header file in which system()is declared, the compilation fails.

为了能够编译使用您没有(手动)声明自己的函数的 C++ 代码,您必须引入声明。这些声明通常存储在所谓的头文件中,您可以使用#include预处理器指令将其拉入当前翻译单元。由于代码没有声明#include的头文件system(),编译失败。

To fix this issue, find out which header file provides you with the declaration of system()and include that. As mentioned in several other answers, you most likely want to add #include <cstdlib>

要解决此问题,请找出哪个头文件为您提供了声明system()并包含它。正如其他几个答案中提到的,您很可能想添加#include <cstdlib>