为什么这个简单的C++程序不能编译?

Why won't this simple C++ program compile?

本文关键字:程序 不能 编译 C++ 简单 为什么      更新时间:2023-10-16

这是我非常简单的C++函数:

#include <string.h>

void myFunc(void * param)
{
        string command;
}

为什么它不编译?

% CC -c -o testFunc.o testFunc.C
"testFunc.C", line 6: Error: string is not defined.
1 Error(s) detected.

<string.h>来自C,定义了C字符串处理函数,如memcmpstrcpy,而不是C++类string。在标准C++中,它的头是<string>,类string在名称空间std中。

它并不是因为它告诉你的原因而编译的:

Error: string is not defined.

<string.h>更改为<string>

还要确保您使用的是正确的命名空间。您可以通过以下方式完成:

using std::string;

std::string command;

更多解释:

  • <string.h>用于C中的C字符串
  • CCD_ 11用于C++中的C字符串
  • CCD_ 12用于C++CCD_