2012-09-10 5 views
-2

나는 몇 가지 컴파일 오류를 발생시키고 그것들을 파악할 수 없다. 아마도 뭔가 단순한 것이지만 그것을 이해할 수 없다. 포인터에 대한 참조가 변경된 것 같아요. 그러나 정확히 무엇을 모릅니다. 포인터를 변경하려고했지만 오류가 발생했습니다. 도움을 주시면 감사하겠습니다. 이러한 오류는 다음과 같습니다메서드 내에서 포인터와 함수 호출

passing argument 1 of 'compare' from incompatible pointer type 
    expected 'struct person *' but argument is of type 'char *' 
    passing argument 2 of 'compare' from incompatible pointer type 
    expected 'struct person *' but argument is of type 'char *' 


    struct person *insert(struct person *head, char *personName, int personAge, int  (*compare)(struct person *a, struct person *b)) 
    { 
    struct person *new; 

    new = (struct person*)malloc(sizeof(struct person)); 
    if(new == NULL) 
    fprintf(stderr,"Couldn't allocate memory!"); 

    new->name = personName; 
    new->age = personAge; 

    if(head == NULL) 
    { 
     new->next = head; 
     head = new; 
    } 

    else 
    { 
     while(head != NULL) 
     head = head->next; 

     //compile errors 
     if(compare(new->name,head->name) < 0) 
     { 
     new->next=head; 
    head->next=NULL; 
     } 

     else 
     { 
     head->next = new; 
     new->next = NULL; 
     } 
    }//else 
     return head; 

    }//method 



    //----------------------------compare--------------------------------// 
    int compare(struct person *a, struct person *b) 
    { 
     int result = strcmp(a->name, b->name); 
     return result; 
    } 
+2

그냥 팁 : 변수 이름을 'new'로 지정하지 마십시오. 이 코드베이스에서 C++을 사용하고 싶다면 변수의 이름을 변경하는 것이 좋습니다. –

+0

나는 이것을 염두에 두겠다. –

+0

@ 리차드 J. 로스 (Richard J. Ross) III : 나는 아주 똑같은 말을 쓰기를 원했다. 어쨌든'delete','class','template'와 같은 다른 C++ 키워드들도 마찬가지입니다. – petersohn

답변

1

귀하의 compare 기능이 person 포인터를 걸리지 만 당신은 2 char 포인터 (이름)을 통과하고 있습니다.

+0

오, 예 ..... 캐스팅을 제안한다고 생각했다. –

2

당신이 compare(new,head) 대신 compare(new->name,head->name)를 사용하지 말아야 compare(new->name,head->name)

+0

오, 그래 ..... 고마워! –

2

compare(new, head)에 대체하려고?

+0

오, 그래 ..... 고마워! –

관련 문제