strcpy 没有在此范围内声明?
strcpy was not declared in this scope?
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Buffer[20] = {'[=10=]'};
cout << "Enter a line of text: " << endl;
string LineEntered;
getline (cin, LineEntered);
if ( LineEntered.length() < 20 ){
strcpy(Buffer, LineEntered.c_str()); // This strcpy. is not declared in scope for some reason.
cout << "Buffer contains: " << Buffer << endl;
}
return 0;
}
这是错误:
main.cpp:14:43: error: 'strcpy' was not declared in this scope
为什么会出现这个错误?
strcpy
函数在包含文件 string.h
中。所以添加:
#include <string.h>
或者,如果您想了解更多 C++,
#include <cstring>
并使用 std::strcpy()
.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Buffer[20] = {'[=10=]'};
cout << "Enter a line of text: " << endl;
string LineEntered;
getline (cin, LineEntered);
if ( LineEntered.length() < 20 ){
strcpy(Buffer, LineEntered.c_str()); // This strcpy. is not declared in scope for some reason.
cout << "Buffer contains: " << Buffer << endl;
}
return 0;
}
这是错误:
main.cpp:14:43: error: 'strcpy' was not declared in this scope
为什么会出现这个错误?
strcpy
函数在包含文件 string.h
中。所以添加:
#include <string.h>
或者,如果您想了解更多 C++,
#include <cstring>
并使用 std::strcpy()
.