C++ 程序中的错误“;错误C3861:&x27;复制字符串';:未找到标识符";,请告诉我为什么?

C++ 程序中的错误“;错误C3861:&x27;复制字符串';:未找到标识符";,请告诉我为什么?,c++,C++,我是编程新手,我曾试图用这个程序将一个字符串复制到另一个字符串中,但它显示出错误 "error C3861: 'copyString': identifier not found" 这是我写的代码 #include <iostream> using namespace std; int main() { char a[8], b[8]; cout << "enter the string a"; cin.get(a, 8); cout

我是编程新手,我曾试图用这个程序将一个字符串复制到另一个字符串中,但它显示出错误

 "error C3861: 'copyString': identifier not found"
这是我写的代码

#include <iostream>
using namespace std;
int main()
{
    char a[8], b[8];
    cout << "enter the string a";
    cin.get(a, 8);
    cout << a;
    int len = sizeof(a) / sizeof(char);
    copyString(a, b);
    int i;
    cin >> i;
    return 0;
}

/*function that copy one string to another*/

void copyString(char* a, char* b)
{
    int i = 0;
    while (a[i] != '\0') {
        b[i] = a[i];
        i++;
    }
    cout << b << " String is this";
}
#包括
使用名称空间std;
int main()
{
字符a[8],b[8];
我不能;
返回0;
}
/*将一个字符串复制到另一个字符串的函数*/
无效复制字符串(字符*a,字符*b)
{
int i=0;
而(a[i]!='\0'){
b[i]=a[i];
i++;
}

不能在
main
之前提供
copyString
实现,或者首先为其提供原型:

void copyString(char *a,char *b); // prototype of copyString
int main()
{ 
    ...
}
void copyString(char *a,char *b)  // implementation of copyString
{
    ...
} 

main
之前提供
copyString
实现,或者首先为其提供原型:

void copyString(char *a,char *b); // prototype of copyString
int main()
{ 
    ...
}
void copyString(char *a,char *b)  // implementation of copyString
{
    ...
} 

C++编译器要求您在使用函数或变量之前提供声明。要解决您的问题,只需在main()之前放置copyString()的正向声明

my_string_lib.cpp


请确保main.cpp、my_string_lib.cpp和my_string_lib.h放在同一个目录中。

C++编译器要求您在使用函数或变量之前提供声明。要解决您的问题,只需在main()之前放置copyString()的正向声明

my_string_lib.cpp


请确保main.cpp、my_string_lib.cpp和my_string_lib.h位于同一目录中。

转发声明不是您的主要问题,但仍需要解决:
cin.get
不读取以空结尾的字符串。您需要手动添加空终止符(无缓冲区溢出)在调用检查空终止符的
copyString
之前。此问题会带回记忆!转发声明不是您的主要问题,但仍需要解决:
cin.get
不读取空终止符字符串。您需要手动添加空终止符(无缓冲区溢出)在调用检查空终止符的
copyString
之前。这个问题会带回记忆!在这种情况下,我更愿意交换函数的顺序。消除了对转发声明进行更改但忘记对实现执行相同操作的可能性。@user4581301同意,对于这个简单的情况,我们可能无法实现y需要prototype。但是对于更长的源代码,prototype确实有助于阅读。在这种情况下,我更愿意交换函数的顺序。消除了更改转发声明但忘记对实现执行相同操作的可能性。@user4581301同意,对于这种简单的情况,我们可能并不真正需要prototype。But对于更长的源代码,原型将真正有助于阅读。
#include <iostream>
using namespace std;

#include "my_string_lib.h" // PLEASE notice this line

int main()
{
    char a[8], b[8];
    cout << "enter the string a";
    cin.get(a, 8);
    cout << a;
    int len = sizeof(a) / sizeof(char);

    copyString(a, b); // included in my_string_lib.h
    int i;
    cin >> i;
    return 0;
}
#pragma once // assume you are using msvc

/*!
    Copy the string content from a to b, 
    assume b is allocated large enough to hold a.
*/
void copyString(char* a, char* b);
#include "my_string_lib.h"

void copyString(char* a, char* b)
{
    /* here is the real implementations for copying a to b*/
}