2015-01-23 3 views
1

기본적으로 나는 5 개의 다른 "웹 사이트"에 대한 암호 관리자 역할을하는 프로그램을 만들어야합니다. 함수와 주요 메소드를 하나의 파일에 모두 선언하면 완벽하게 실행됩니다. 의 요소 : 나는 헤더 파일을 사용하지만 때 나는배열 요소에 불완전한 타입이 있습니다.

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

#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를

#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 표시되는 오류 배열 '입력 사이트 [5]'형식이 불완전합니다

다른 IDE에서 프로그램을 빌드하려고 시도한 결과, 배열 크기가 너무 커서 5 개의 구조체가 분명히 구성되었다고합니다. 나는 모든 것이 하나의 파일에있을 때 완벽하게 실행된다고 말했기 때문에 내가 가지고있는 코드가 작동한다는 것을 알고있다.

+0

'main()'이'struct entry' *가 어떻게 생겼는지에 대해 알고있는 자신에게 물어보십시오. 그것의 정의 *는 또 다른 .c 파일에 묻혀있다. 당신이 이것을 할 것인지를 알아야 할 필요가 있습니다 :'struct entry sites [5];' – WhozCraig

+0

"struct entry"의 정의를 .c 파일로 옮깁니다. –

답변

1

이러한 분리 문제는 struct entry의 내부 구조가 functions.c 번역 단위에 비공개가된다는 것입니다. 이것은 바람직하지 않을 수도 있습니다. 당신이 당신의 struct 노출 괜찮다면 동적 할당

  • struct 개인, 스위치를 유지하고자하는 경우

    • , 헤더 파일에 정의를 이동합니다.

    다음은 첫 번째 방법입니다 : malloc(sizeof(struct entry)*count)를 호출하여 functions.c 파일에이 함수를 정의 헤더

    struct entry *allocateEntries(size_t count); 
    

    에 기능을 추가 할 수 있습니다. 지금 당신은 main()의 끝에 free(sites)을 추가하는 것을 잊지 마십시오

    struct entry *sites = allocateEntries(5); 
    

    struct entry sites[5]; 
    

    을 대체 할 수 있습니다.

  • +0

    struct의 정의를 내 헤더 파일로 옮겼습니다. 메신저에 "requestEntry (entry *)에 대한 정의되지 않은 참조"라는 오류가 발생하여 모든 내 기능에 표시됩니다. –

    +1

    @HunterTipton 이상합니다. 실수로'#include' 라인을'struct' 정의의 다섯 줄과 함께 움직이지 않았습니까? – dasblinkenlight

    +0

    아니요, 구조체 정의를 옮겼습니다. –

    1

    struct entry의 정의가 표시되지 않는 어딘가에 struct entry의 배열을 선언 할 수 없습니다. 컴파일러는 배열의 각 요소를 만드는 것이 얼마나 큰지 알지 못합니다.

    직접적인 경우는 struct entry의 정의를 functions.c에서 functions.h로 이동하는 것입니다.

    관련 문제