So basically i'm supposed to make a program that acts as a password manager for 5 different "websites". When I declare the functions and main method all in one file it runs perfectly. However when I use a header file I get the error shown
所以基本上我应该制作一个程序,作为5个不同“网站”的密码管理器。当我在一个文件中声明函数和main方法时,它运行得很完美。但是,当我使用头文件时,我收到错误
functions.h
functions.h
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
struct entry;
void requestEntry(struct entry *data);
void displaySummary(struct entry *data);
void displayEntry(struct entry *data);
#endif
functions.c
functions.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);
}
main.c
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]);
}
}
ERROR: elements of array 'entry sites [5]' have incomplete type
错误:数组'入口站点[5]'的元素具有不完整的类型
I tried building the program in a different IDE and it said that my array size was too large, when its obviously only 5 structs. I know that the code I have does work because like I said it runs perfectly when everything is in one file.
我尝试在不同的IDE中构建程序,它说我的数组大小太大,显然只有5个结构。我知道我所拥有的代码确实有效,因为就像我说的那样,当一切都在一个文件中时,它运行得很好。
2 个解决方案
#1
1
The problem with such separation is that the inner structure of your struct entry
becomes private to the functions.c
translation unit. This may or may not be desirable.
这种分离的问题在于,struct entry的内部结构对于functions.c转换单元是私有的。这可能是也可能不是所希望的。
- If you wish to keep the
struct
private, switch to dynamic allocation - 如果您希望保持结构私有,请切换到动态分配
- If you do not mind exposing your
struct
, move its definition to the header file. - 如果您不介意公开结构,请将其定义移动到头文件中。
Here is the first approach: add a function to the header
这是第一种方法:向标题添加一个函数
struct entry *allocateEntries(size_t count);
Define this function in your functions.c
file by calling malloc(sizeof(struct entry)*count)
. Now you can replace
通过调用malloc(sizeof(struct entry)* count)在functions.c文件中定义此函数。现在你可以替换
struct entry sites[5];
with
同
struct entry *sites = allocateEntries(5);
Don't forget to add free(sites)
to the end of main()
.
不要忘记在main()的末尾添加免费(站点)。
#2
1
You can't declare an array of struct entry
somewhere where the definition of struct entry
isn't visible; the compiler doesn't know how big to make each element of the array.
你不能在结构条目的定义不可见的地方声明一个struct entry数组;编译器不知道制作数组的每个元素有多大。
The straightforward thing, in your case, is to move the definition of struct entry
from functions.c to functions.h.
在您的情况下,直接的事情是将struct entry的定义从functions.c移动到functions.h。
#1
1
The problem with such separation is that the inner structure of your struct entry
becomes private to the functions.c
translation unit. This may or may not be desirable.
这种分离的问题在于,struct entry的内部结构对于functions.c转换单元是私有的。这可能是也可能不是所希望的。
- If you wish to keep the
struct
private, switch to dynamic allocation - 如果您希望保持结构私有,请切换到动态分配
- If you do not mind exposing your
struct
, move its definition to the header file. - 如果您不介意公开结构,请将其定义移动到头文件中。
Here is the first approach: add a function to the header
这是第一种方法:向标题添加一个函数
struct entry *allocateEntries(size_t count);
Define this function in your functions.c
file by calling malloc(sizeof(struct entry)*count)
. Now you can replace
通过调用malloc(sizeof(struct entry)* count)在functions.c文件中定义此函数。现在你可以替换
struct entry sites[5];
with
同
struct entry *sites = allocateEntries(5);
Don't forget to add free(sites)
to the end of main()
.
不要忘记在main()的末尾添加免费(站点)。
#2
1
You can't declare an array of struct entry
somewhere where the definition of struct entry
isn't visible; the compiler doesn't know how big to make each element of the array.
你不能在结构条目的定义不可见的地方声明一个struct entry数组;编译器不知道制作数组的每个元素有多大。
The straightforward thing, in your case, is to move the definition of struct entry
from functions.c to functions.h.
在您的情况下,直接的事情是将struct entry的定义从functions.c移动到functions.h。