これが私の非常に単純な 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.
これが私の非常に単純な 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++ クラスではなく、memcmp
や などのC 文字列処理関数を定義します。標準 C++ では、そのヘッダーはであり、クラスは名前空間にあります。strcpy
string
<string>
string
std
まさにそれがあなたに言っている理由でコンパイルされていません:
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
。