0

这是我非常简单的 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.
4

2 回答 2

8

<string.h>来自 C 并定义了 C 字符串处理函数,例如memcmpand strcpy,而不是 C++ 类string。在标准 C++ 中,它的标头是<string>,并且类string在 namespace 中std

于 2013-08-26T20:37:56.023 回答
6

它没有编译它告诉你的确切原因:

Error: string is not defined.

更改<string.h><string>

还要确保您使用的是正确的命名空间。您可以通过以下方式做到这一点:

using std::string;

或者

std::string command;

更多解释:

  • <string.h>用于 C 中的 C 字符串。
  • <cstring>用于 C++ 中的 C 字符串。
  • <string>适用于 C++ std::string
于 2013-08-26T20:38:40.750 回答