C++ 创建我自己的strcopy函数
我正在尝试创建自己的str copy函数。我得到一个错误,告诉我strcopy不在这个范围内 strcopydeck[i].西服,来源[];我的错误发生了。救命啊C++ 创建我自己的strcopy函数,c++,C++,我正在尝试创建自己的str copy函数。我得到一个错误,告诉我strcopy不在这个范围内 strcopydeck[i].西服,来源[];我的错误发生了。救命啊 #include <fstream> #include <iostream> using namespace std; struct{ char suit[]; char rank[]; int cvalue; }; int main() { char source[] = "name"; cards dec
#include <fstream>
#include <iostream>
using namespace std;
struct{
char suit[];
char rank[];
int cvalue;
};
int main()
{
char source[] = "name";
cards deck[52];
strcopy(deck[].suit,source[]);
}
void strcopy(char destination[], char source[])
{
for(int i=0; source[i] != '\0' && destination[i] != '\0'; i++)
{
destination[i] = source[i];
}
}
首先,您忘记指定结构的名称。我想应该有名片 此外,结构中定义的数组必须定义为具有大小
struct{
char suit[]; // What is the size of the array?
char rank[]; // What is the size of the array?
int cvalue;
};
函数strcopy必须在isage之前声明
函数本身是错误的
我不会发明一种新的函数算法,并按照下面的方式编写它
char * strcopy( char destination[], const char source[] )
{
char *p = destination;
while ( *p++ = *source++ );
return destination;
}
在编译器知道存在strcopy函数之前,您正在使用strcopy函数。在C/C++中,您必须在顶部定义它,或者通过提供函数的原型来提供存在该函数的信息 所以要么像这样移动它:
void strcopy(char destination[], char source[])
{
for(int i=0; source[i] != '\0' && destination[i] != '\0'; i++)
{
destination[i] = source[i];
}
}
int main()
{
char source[] = "name";
cards deck[52];
strcopy(deck[].suit,source[]);
}
或在使用前添加原型:
void strcopy(char destination[], char source[]);
int main()
{
char source[] = "name";
cards deck[52];
strcopy(deck[].suit,source[]);
}
void strcopy(char destination[], char source[])
{
for(int i=0; source[i] != '\0' && destination[i] != '\0'; i++)
{
destination[i] = source[i];
}
}
有关更多信息,您也可以在wiki上看到:
我还构建了代码以提高可读性 编译器错误是正确的。strcopy不会在您尝试使用它时声明。它是声明后*,但C++编译器是幼稚的,它需要知道函数存在于它被命名的点。请谷歌至少你的错误。谢谢所有的帮助,我学习代码,所以我确实犯一些新手错误。杰克林,去做点运动什么的。真的没有必要躲在你的电脑后面做鲁迪奥凯,让我们再试一次。我投票结束了你的问题,你投了两张反对票。你的大多数其他问题的反对票在-1到0之间。如果你继续发布这种质量的问题,你将被禁止提问。我重新键入了它,我在我的真实电脑上发布了它,并在virutal box中编程,这样做更简单,一定没有卡片。我在数组中指定了值。我的动作功能怎么了?@jalf该功能没有意义。首先,如果目标数组以零开始,并且不复制终止的零,那么它将不复制任何内容。