나는 몇 가지 컴파일 오류를 발생시키고 그것들을 파악할 수 없다. 아마도 뭔가 단순한 것이지만 그것을 이해할 수 없다. 포인터에 대한 참조가 변경된 것 같아요. 그러나 정확히 무엇을 모릅니다. 포인터를 변경하려고했지만 오류가 발생했습니다. 도움을 주시면 감사하겠습니다. 이러한 오류는 다음과 같습니다메서드 내에서 포인터와 함수 호출
는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;
}
그냥 팁 : 변수 이름을 'new'로 지정하지 마십시오. 이 코드베이스에서 C++을 사용하고 싶다면 변수의 이름을 변경하는 것이 좋습니다. –
나는 이것을 염두에 두겠다. –
@ 리차드 J. 로스 (Richard J. Ross) III : 나는 아주 똑같은 말을 쓰기를 원했다. 어쨌든'delete','class','template'와 같은 다른 C++ 키워드들도 마찬가지입니다. – petersohn