這裏是我最簡單的C++函數:爲什麼不能編譯這個簡單的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++函數:爲什麼不能編譯這個簡單的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和定義像memcmp
和strcpy
,而不是C++ string
C類字符串處理函數。在標準C++中,標題爲<string>
,類string
位於命名空間std
中。
-1:
@ThomasEding:好的。我腦海中的那個標題是'
我認爲你有一個額外的「和」在開頭附近:-) – UpAndAdam
它不是一種編譯完全原因,它告訴你:
Error: string is not defined.
變化<string.h>
到<string>
。
還要確保您使用的是正確的名稱空間。你可以這樣做:
using std::string;
或
std::string command;
更多的解釋:
<string.h>
是在C.<cstring>
是對C 〜應變gs在C++中。<string>
適用於C++ std::string
。
儘管名稱中沒有在名稱爲「string」的''頭部中定義的實體, –
你很可能想'#包括'並且使用'std :: string命令'而不是'string command'。此外,我會確保CC是一個C++編譯器,而不是C編譯器。 –
爲什麼倒票 - 它是一個非常好的問題,並帶有明確的答案 – pm100