简体   繁体   English

没有在此范围内声明strcpy吗?

[英]strcpy was not declared in this scope?

#include <iostream>
#include <string>
using namespace std;

int main()
{
    char Buffer[20] = {'\0'};

    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;
}

This is the error: 这是错误:

main.cpp:14:43: error: 'strcpy' was not declared in this scope

Why is it having this error? 为什么会出现此错误?

The strcpy function is in the include file string.h . strcpy函数位于包含文件string.h So add: 因此添加:

#include <string.h>

Or, alternatively if you want to be more C++ about it, 或者,如果您想更多地使用C ++,

#include <cstring>

and use std::strcpy() . 并使用std::strcpy()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM