有人可以解释为什么当我使用以下 g++ 编译器编译源代码时出现此错误
#include <cstdio>
#include <string>
using namespace std;
int main()
{
char source_language[50];
scanf("%16s\n",source_language);
int length = sizeof(source_language);
int sizeofchar = strlen(source_language);
printf("%d\n",sizeofchar);
}
这给了我以下错误
test.cpp:在函数‘int main()’中:
test.cpp:31:错误:“strlen”未在此范围内声明
当我改变#include <string>
into #include <string.h>
or #include<cstring>
,它工作正常,我需要弄清楚使用有什么区别#include<string>
and #include<string.h>
。非常感谢任何帮助
您正在尝试使用strlen
函数,其声明于string.h
(或者,作为命名空间的成员std
in cstring
)。所以,为了使用strlen
您应该包含这两个标头之一。
The #include <string>
变体不起作用只是因为string
是一个完全不相关的 C++ 特定头文件,它绝对具有nothing与 C 标准库字符串函数有关。是什么让您期望它会起作用?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)