我的功能拒绝存在

发布于 2025-01-07 16:10:31 字数 2825 浏览 0 评论 0原文

每当我尝试运行我的测试文件时,我都会收到此错误:

/tmp/ccCazDOe.o: In function `main':
/home/cs136/cs136Assignments/a06/testing.c:8: undefined reference to `icopy'
collect2: ld returned 1 exit status

该代码旨在实现 C 中的列表结构。像 icon_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;
}

}

Whenever I try to run my test file, I get this error:

/tmp/ccCazDOe.o: In function `main':
/home/cs136/cs136Assignments/a06/testing.c:8: undefined reference to `icopy'
collect2: ld returned 1 exit status

The code is intended to implement a list structure in C. Functions like icons_destroy and irest_destroy are meant to be destructive functions.

The same thing happens when I try to use my ilength function.

I have tried to rewrite my functions, renaming the functions, defining them multiple times in the header, making a new test file. I just can't seem to find out what is wrong. It appears to work when I decided to make a function called ilength that just returns a number, so I think it might be a problem with the way the function works.

Any help here?

The code of my test file:

#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);
}

My header file:

// 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);

My .c file:

#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;
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烂柯人 2025-01-14 16:10:31

看起来您可能只编译 testing.c,而不是 ilist_pressive.c。您需要使用这样的命令来编译它们:(

gcc -Wall testing.c ilist_destructive.c -o testing

同时编译和链接它们)或者使用这样的一系列命令:(

gcc -Wall testing.c -c testing.o
gcc -Wall ilist_destructive.c -c ilist_destructive.o
gcc testing.o ilist_destructive.o -o testing

将它们中的每一个编译成一个目标文件,然后链接然后将它们放在一起;这更加灵活,因为如果相关源文件没有更改,您可以放弃前两个步骤中的任何一个)。

It looks like you're probably only compiling testing.c, not ilist_destructive.c. You need to compile both of them, with a command like this:

gcc -Wall testing.c ilist_destructive.c -o testing

(which compiles and links them both at the same time) or else with a series of commands like this:

gcc -Wall testing.c -c testing.o
gcc -Wall ilist_destructive.c -c ilist_destructive.o
gcc testing.o ilist_destructive.o -o testing

(which compiles each of them into an object file, and then links them together afterward; this is a bit more flexible, since you can forgo either of the first two steps if none of the relevant source-files has changed).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文