2012-03-02 3 views
0

좋아요, main.c이라는 메인 소스와 test.h이라는 헤더 파일, handBeraknare.c이라는 또 다른 클래스가 있습니다. 내 메소드 중 일부를 클래스 handBeraknare.c으로 transfeering하여 코드를 좀 더 읽기 쉽게하려고합니다. main.c에서헤더 파일을 만드는 데 문제가 있습니다.

typedef struct kort{ 
    int draget; 
    char farg; 
    int nummer; 
    struct kort *next; 
    }kort; ` 

내가이 사용 kort k=(kort*)malloc(sizeof(kort));의 몇 가지를 만들고 배열에 넣어 :

그래서 main.c에서 나는 다음과 같습니다 구조체가있다. 무엇을 달성하려고 노력하고 handBeraknare.c에있는 함수에 kort 배열을 보내고 있지만 이상한 오류 "in file included from handBeraknare.c" 일종 얻을.

Im gussing이 헤더 파일과 관련이 있습니다. 이제는 "kort"이 (내 구조체)입니다. 어쨌든, 여기에 코드의 일부는 다음과 같습니다

// in test.h 
int beraknaFarg(kort kortHand[]); 



// in handBeraknare.c 
#include <stdio.h> 
#include "test.h" 
int beraknaFarg(kort kortHand[]){ 
char c = kortHand[0].farg; 
    int i; 
    for (i=1;i<5;i++){ 
     if (kortHand[i].farg!=c){ 
           printf("inte färg"); 
           system("pause"); 
      //Spelaren har inte färg. Retunera 0 
      return 0; 
      } 
     } 
     //Spelaren har färg. Retunera 1 
     printf("!!!!färg"); 
           system("pause"); 
     return 1; 
} 


//part of the main class. Calling function test() 
// which calls the method beraknaHand which exists in handBeraknare.c 

#include "test.h" 
... 

int main(int argc, char *argv[]) 
{ 
    test(); 
} 

// the testfunction in my mainclass 
void test(){ 
     char farg[4]={'S','K','R','J'}; 
     int nummer[14]={0,2,3,4,5,6,7,8,9,10,11,12,13,14}; 
     kort kortArray[52]; 
     kort kortHand[5]; 
        kort *k; 
        k=(kort*)malloc(sizeof(kort));    
        k->farg='s'; 
        k->nummer=5; 
        kortHand[0]=*k; 

        k->farg='s'; 
        k->nummer=11; 
        kortHand[1]=*k; 

        k->farg='s'; 
        k->nummer=12; 
        kortHand[2]=*k; 

        k->farg='s'; 
        k->nummer=11; 
        kortHand[3]=*k; 

        k->farg='s'; 
        k->nummer=9; 
        kortHand[4]=*k; 
    beraknaFarg(kortHand); 
+0

정보가 충분하지 않습니다. handBeraknare.h는 어떻게 생겼습니까? 정확한 오류 메시지를 알려주십시오. –

답변

2

만들기 test.h 당신은 헤더 파일의 형식 정의를 정의 할 필요가 main.c를

+0

제대로 작동합니다. 이것은 단순히 구문 오류의 문제였습니다. 내 머리글에 typedef를 선언하고 mainley에서 completley를 제거했습니다. 감사! –

1

에서 typedef

typedef struct kort{ 
     int draget; 
     char farg; 
     int nummer; 
     struct kort *next; 
     } kort; 
int beraknaFarg(kort kortHand[]); 

를 읽고 제거 , 그리고 헤더 파일을 C 파일에 포함 시켜서 사용하길 바란다. 또한 typedef 정의이고 선언이 아니기 때문에 C 파일에 정의해야합니다. 즉
시간 파일 :

typedef strcut 
{ 
    int a; 
    .... 
}t_struct_type; 

C 파일 : 둘 이상의 C 파일에 struct_var을 사용하려는 경우

t_struct_type struct_var; 

, 당신은 시간의 파일에 extern 키워드를 추가해야합니다. extern t_strcut_type struct_var

+0

답변에 약간 혼란 스러웠습니다. +1은 작동하는 솔루션입니다. 메인 클래스에서 전체 typedef 선언을 제거하고 대신 헤더 파일에 선언했습니다. extern 키워드를 사용하지 않고 main과 handberaknare의 struct를 사용하여 그 메신저를 추가 할 수 있습니다 :) –

관련 문제