【问题标题】:strcpy was not declared in this scope?strcpy 没有在这个范围内声明?
【发布时间】:2015-10-03 18:27:25
【问题描述】:
#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;
}

这是错误:

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

为什么会出现这个错误?

【问题讨论】:

    标签: c++ strcpy


    【解决方案1】:

    strcpy 函数在包含文件string.h 中。所以添加:

    #include <string.h>
    

    或者,或者,如果您想更多地使用 C++,

    #include <cstring>
    

    并使用std::strcpy()

    【讨论】:

    • "如果你想更多地了解它" — 如果你不想使用不推荐使用的标头。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多