我在系统行C++上收到错误

I get error on system line C++

本文关键字:错误 C++ 系统      更新时间:2023-10-16

我有一个用C++编写的非常简单的程序,请看这里:

#include <iostream>
using namespace std;
int main()
{
cout<<"Simple message"<<endl;
system("msg * test message");
return 0;
}

当我尝试使用命令g++ 1.cpp -o test.exe编译这个脚本时,我得到错误:

1.cpp: In function 'int main()':
1.cpp:6:29: error: 'system' was not declared in this scope
  system("msg * test message");
                         ^

我检查了代码,但找不到这个错误的原因,我应该更改编译器还是这个代码中有错误?

system()stdlib.h中定义(对于C++,则为cstdlib)。

#include <cstdlib>

您需要包含system函数所在的库头。

将此添加到顶部:

#include <stdlib.h>