C 数组元素的类型不完整
所以基本上我要做一个程序,作为5个不同“网站”的密码管理器。当我在一个文件中声明函数和main方法时,它运行得非常好。但是,当我使用头文件时,会显示错误 功能C 数组元素的类型不完整,c,arrays,C,Arrays,所以基本上我要做一个程序,作为5个不同“网站”的密码管理器。当我在一个文件中声明函数和main方法时,它运行得非常好。但是,当我使用头文件时,会显示错误 功能 #ifndef FUNCTIONS_H_ #define FUNCTIONS_H_ struct entry; void requestEntry(struct entry *data); void displaySummary(struct entry *data); void displayEntry(struct entry
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
struct entry;
void requestEntry(struct entry *data);
void displaySummary(struct entry *data);
void displayEntry(struct entry *data);
#endif
职能.c
#include <stdio.h>
#include "functions.h"
struct entry{
char webName[32];
char userName[32];
char password[32];
};
void requestEntry(struct entry *data){
printf("Please enter the following items: \n");
printf("Website name: ");
scanf("%s", data->webName);
printf("Username: ");
scanf("%s", data->userName);
printf("Password: ");
scanf("%s", data->password);
}
void displaySummary(struct entry *data){
printf(" - %s\n", data->webName);
}
void displayEntry(struct entry *data){
printf("Website: %s\n", data->webName);
printf("Username: %s\n", data->userName);
printf("Password: %s\n", data->password);
}
#包括
#包括“functions.h”
结构条目{
char-webName[32];
字符用户名[32];
字符密码[32];
};
无效请求条目(结构条目*数据){
printf(“请输入以下项目:\n”);
printf(“网站名称:”);
scanf(“%s”,数据->网站名);
printf(“用户名:”);
scanf(“%s”,数据->用户名);
printf(“密码:”);
scanf(“%s”,数据->密码);
}
无效显示摘要(结构条目*数据){
printf(“-%s\n”,数据->网站名);
}
无效显示条目(结构条目*数据){
printf(“网站:%s\n”,数据->网站名);
printf(“用户名:%s\n”,数据->用户名);
printf(“密码:%s\n”,数据->密码);
}
main.c
#include <stdio.h>
#include <stdbool.h>
#include "functions.h"
int main()
{
struct entry sites[5];
for (int i = 0; i < 5; i++){
requestEntry(&sites[i]);
}
printf("\n");
printf("Summary: \n");
for (int i = 0; i < 5; i++){
printf("%d", (i + 1));
displaySummary(&sites[i]);
}
bool cont = true;
int i;
while (cont){
printf("Type in a number from 1 to 5 to pull up the entry, or type 0 to exit: ");
scanf("%d", &i);
if (i == 0){
cont = false;
continue;
}
printf("\n");
displayEntry(&sites[i - 1]);
}
}
#包括
#包括
#包括“functions.h”
int main()
{
结构入口站点[5];
对于(int i=0;i<5;i++){
请求输入(&sites[i]);
}
printf(“\n”);
printf(“摘要:\n”);
对于(int i=0;i<5;i++){
printf(“%d”,(i+1));
显示摘要(&sites[i]);
}
bool cont=真;
int i;
while(续){
printf(“键入1到5之间的数字以调出条目,或键入0以退出:”;
scanf(“%d”、&i);
如果(i==0){
cont=假;
继续;
}
printf(“\n”);
显示条目和站点[i-1];
}
}
错误:数组“入口站点[5]”的元素的类型不完整
我尝试在另一个IDE中构建程序,它说我的数组太大了,而它显然只有5个结构。我知道我的代码确实有效,因为正如我所说的,当所有内容都在一个文件中时,它可以完美地运行 在
struct entry
的定义不可见的地方,不能声明struct entry
的数组;编译器不知道数组的每个元素的大小
在您的例子中,最简单的事情是将
struct entry
的定义从functions.c移到functions.h.这种分离的问题是struct entry
的内部结构对于functions.c
翻译单元是私有的。这可能是可取的,也可能不是可取的
- 如果希望将结构保持为私有,请切换到动态分配
- 如果不介意公开
,请将其定义移动到头文件结构
struct entry *allocateEntries(size_t count);
通过调用malloc(sizeof(struct entry)*count)
在functions.c
文件中定义此函数。现在你可以替换
struct entry sites[5];
与
别忘了在
main()
的末尾添加free(sites)
问问自己main()
对struct条目的了解如何。它的定义隐藏在另一个.c文件中。需要知道是否要这样做:struct entry sites[5]代码>尝试将“struct entry”的定义移到.c文件我将结构的定义移到了头文件中,但是现在我收到了错误,指出“对`requestEntry(entry*)的引用未定义”,这在我的所有文件中都会出现functions@HunterTipton真奇怪。您没有意外地将#include
行与结构定义的五行一起移动,是吗?没有,我只是移动了结构definition@HunterTipton我刚刚尝试了这个-用定义替换了struct
声明,并从.c文件中删除了定义。代码编译没有任何问题。实际上发生了一些奇怪的事情,当我在一个IDE(code::Blocks)上运行代码时,它似乎给出了我提到的错误。然而,当我在另一个IDE(MicrosoftVisualStudio2013)上运行它时,它工作得完美无缺,这是在移动了结构定义之后
struct entry *sites = allocateEntries(5);