|
서찬 님이 쓰신 글 :
: linked list를 이용해서 다항식의 덧셈을 구현하고 하는데요.
: 아래와 같은 오류메시지가 출력됩니다.
: 이 구문에서 에러가 발생하는데요..
: a = (poly_pointer)malloc(sizeof(poly_node));
:
: poly_node는 분명히 정의를 해주었는데 왜 이런걸까요?
: 제가 혹시 뭔가 중요한 헤더파일을 빼먹고 있는 걸까요?
: 답변부탁드립니다.
:
: #include <stdio.h>
: #include <stdlib.h>
: #define IS_FULL(ptr) (!(ptr))
:
: typedef struct poly_node *poly_pointer;
: typedef struct poly_node {
: int coef;
: int expon;
: poly_pointer link;
: };
: poly_pointer a, b, d;
:
: poly_pointer padd(poly_pointer a, poly_pointer b);
: void attach(int coefficient, int exponential, poly_pointer *ptr);
: int compare(int x, int y);
:
:
: main() {
: a = (poly_pointer)malloc(sizeof(poly_node));
: attach(3, 14, &a);
: attach(2, 8, &a);
: attach(1, 0, &a);
: printf("%d", &a);
: }
:
: poly_pointer padd(poly_pointer a, poly_pointer b) {
: poly_pointer front, rear, temp;
: int sum;
: rear = (poly_pointer)malloc(sizeof(poly_node));
: if(IS_FULL(rear)) {
: fprintf(stderr, "The memory is full\n");
: exit(1);
: }
: front = rear;
: while(a && b)
: switch(compare(a -> expon, b -> expon)) {
: case -1:
: attach(b -> coef, b -> expon, &rear);
: b = b -> link;
: break;
: case 0:
: sum = a -> coef + b -> coef;
: if(sum) attach(sum, a -> expon, &rear);
: a = a -> link; b = b -> link;
: break;
: case 1:
: attach(a -> coef, a -> expon, &rear);
: a = a -> link;
: }
: /* 리스트 a와 리스트 b의 나머지를 복사하기 위한 부분 */
: for(;a;a->link) attach(a -> coef, a -> expon, &rear);
: for(;b;b->link) attach(b -> coef, b -> expon, &rear);
: rear -> link = NULL;
: /* 필요없는 초기 노드를 삭제 */
: temp = front; front = front -> link; free(temp);
: return front;
: }
:
: void attach(int coefficient, int exponent, poly_pointer *ptr) {
: poly_pointer temp;
: temp = (poly_pointer)malloc(sizeof(poly_node));
: if(IS_FULL(temp)) {
: fprintf(stderr, "The memory is full\n");
: exit(1);
: }
: temp -> coef = coefficient;
: temp -> expon = exponent;
: (*ptr) -> link = temp;
: *ptr = temp;
: }
:
: int compare(int x, int y) {
: if(x < y)
: return -1;
: else if(x == y)
: return 0;
: else
: return 1;
: }
:
:
:
:
: --------------------Configuration: poly_pointer_padd - Win32 Debug--------------------
: Compiling...
: poly_pointer_padd.c
: C:\......\poly_pointer_padd.c(19) : error C2065: 'poly_node' : undeclared identifier
: Error executing cl.exe.
:
: poly_pointer_padd.obj - 1 error(s), 0 warning(s)
프로그래머의 의도상 poly_node를 재정의 하려고 했던 거 같네요.
이렇게 바꾸는 게 나을 듯 합니다.
typedef struct {
int coef;
int expon;
poly_pointer link;
} poly_node;
이렇게 하면 구조체를 poly_node라는 자료형으로 계속 사용할 수 있습니다.
|