今天在寫程序時遇到的一個問題
#include <stdio.h> #include <string> using std::string; int main() { char str[STEL]; while (scanf("%s", str) && strcmp(str, "end")) { printf("%s = %u\n", str, hash(str)); } return 0; }
寫完用g++編譯,出現error: ‘strcmp’ was not declared in this scope
上網查找發現必須再加上#include <string.h>才能正確編譯執行,即同時存在
#include <string.h> #include <string> using std::string;
也就是說strcmp不在C++標准庫中,需要單獨包含strcmp所在的頭文件。
自己試了下
#include <cstring> using namespace std;
也可以完成,即c的標准庫中也包含這個函數