Всякий раз, когда я пытаюсь запустить свой тестовый файл, я получаю эту ошибку:
/tmp/ccCazDOe.o: In function `main':
/home/cs136/cs136Assignments/a06/testing.c:8: undefined reference to `icopy'
collect2: ld returned 1 exit status
Код предназначен для реализации структуры списка в C. Такие функции, как icons_destroy и irest_destroy, должны быть деструктивными функциями.
То же самое происходит, когда я пытаюсь использовать функцию ilength.
Я попытался переписать свои функции, переименовать функции, определить их несколько раз в заголовке, создать новый тестовый файл. Я просто не могу понять, что не так. Кажется, это работает, когда я решил создать функцию с именем ilength, которая просто возвращает число, поэтому я думаю, что это может быть проблемой из-за того, как эта функция работает.
Любая помощь здесь?
Код моего тестового файла:
#include <stdio.h>
#include "ilist_destructive.h"
int main(void){
ilist x = iempty();
x = icons_destroy(1, x);
x = icons_destroy(2, x);
ilist y = icopy(x);
idelete(y);
idelete(x);
}
Мой заголовочный файл:
// Destructive Abstract data type ilist
struct ilist_ADT;
typedef struct ilist_ADT *ilist;
ilist iempty();
int iempty_huh(ilist il);
int ifirst(ilist il);
ilist icons_destroy(int in, ilist il);
ilist irest_destroy(ilist il);
ilist icopy(ilist il);
int ilength(ilist il);
void idelete(ilist il);
int ilength(ilist il);
ilist icopy(ilist il);
Мой файл .c:
#include "ilist_destructive.h"
#include <stdlib.h>
// The ilist ADT is a pointer to this secret struct
struct ilist_ADT{
struct ilist_ADT *rest;
int first;
};
ilist icons_destroy(int in, ilist il){
if (il == NULL) {
ilist anewlist = malloc(sizeof(struct ilist_ADT));
anewlist->first = in;
anewlist->rest = NULL;
return (anewlist);
} else {
ilist previous = malloc(sizeof(struct ilist_ADT));
previous->first = il->first;
previous->rest = il->rest;
il->first = in;
il->rest = previous;
return il;
}
}
// ifirst returns the first element of il
int ifirst(ilist il){
if(il == NULL){
exit(1);
}
return il->first;
}
ilist irest(ilist il){
if(il == NULL){
exit(1);
}
return il->rest;
}
ilist irest_destroy(ilist il){
if(il == NULL){
exit(1);
}else if(il->rest == NULL){
free(il);
return NULL;
}else{
ilist original = il->rest;
il->first = original->first;
il->rest = original->rest;
free(original);
return il;
}
}
ilist iempty(){
return NULL;
}
// test for empty ilist
int iempty_huh(ilist il){
return il == NULL;
}
// free memory for entire ilist
void idelete(ilist il){
while (il != NULL) {
ilist next = il->rest;
free(il);
il = next;
}
int ilength(ilist il){
int counter = 0;
while (iempty_huh(il) != 1){
counter = counter + 1;
il = irest(il);
}
return counter;
}
ilist icopy(ilist il){
ilist copy = malloc(sizeof(struct ilist_ADT));
copy->first = il->first;
copy->rest = il->rest;
return copy;
}
}