gpt4 book ai didi

c - 我一直收到 collect2 : error: ld returned 1 exit status

转载 作者:太空宇宙 更新时间:2023-11-04 03:52:08 25 4
gpt4 key购买 nike

我遇到了一些问题。每次尝试编译我的程序时,我都会收到“collect2: error: ld returned 1 exit status”。我在谷歌上搜索了这个,查看了示例,发现它通常发生在头文件未正确使用时;但是,我没有在我的代码中使用头文件,所以我不确定为什么要这样做。我认为这对其他遇到此错误但不明白原因的人会有所帮助。我确实知道我的代码中有很多错误,但我正在努力找到可以编译它并解决所有问题的地方。非常感谢!!

#include <stdio.h>
#include <stdlib.h>

struct node_t {
double x;
struct node_t *next;
};

struct node_t *create_node(double n);
void print_node (struct node_t * node );
void print_list (struct node_t * head );
struct node_t * insert_head (struct node_t *head , struct node_t * node );
struct node_t * insert_tail (struct node_t *head , struct node_t * node );
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos );
int count_nodes (struct node_t * head );
struct node_t * delete_node (struct node_t *head , double n);
void delete_list (struct node_t * head );

int main (void)
{

double n;
int pos;


/** Asks the user what they would like to do. */

printf("Please select an option: ");
printf("1. Enter a number ");
printf("2. Delete a number ");
printf("3. Print all numbers ");
printf("4. Tell how many items in the list ");
printf("5. End program ");
printf("Please enter your choice (1, 2, 3, 4, 5): ");
while ((tmp = getchar()) != '\n')
in = tmp; /** Tells the compiler to use the number that the use put in. */

switch (in){
case '1': {/** If the user chooses Enter a Number, go through this process. */
printf("1. Enter item at head of list ");
printf("2. Enter item in middle of list ");
printf("3. Enter item at tail of list ");
printf("Please enter your choice (1,2,3): ");
while ((tmp = getchar()) != '\n')
in = tmp;

switch (in) {
case '1':{
printf("Enter the number to be entered at head: ");

fgets(s, 1024, stdin);

struct node_t * create_node (double n);
struct node_t *insert_head(struct node_t *head, struct node_t *node);

break;
}

case '2':{
printf("Enter the number to be entered in middle: ");

fgets(s, 1024, stdin);

struct node_t * create_node (double n);
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos);
break;
}

case '3':{
printf("Enter the number to be entered at tail: ");

fgets(s, 1024, stdin);

struct node_t * create_node (double n);
struct node_t *insert_tail(struct node_t *tail, struct node_t *node);

break;
}
break;
}
}

case '2':{/** If the user selected Delete a Number, go through this process. */

printf("Enter the node that needs to be deleted: ");

fgets(s, 1024, stdin);

struct node_t * delete_node (struct node_t *head , double n);

break;

}

case '3':{/** If the user selected Print All Numbers, go through this process. */

//void print_list (struct node_t *head)

break;

}

case '4':{ /** If the user selected Tell How Many Items in the List, go through this process. */
int count_nodes (struct node_t * head );

break;
}

case '5':{ /** If the user selected End Program, go through this process. */

return 1;

break;
}

}

return 0;

}







/** Creates a new node
* @param double n, a number n
* @return pointer
*/

struct node_t * create_node (double n)
{


struct node_t *p = malloc (size of (struct node_t))
p -> x = n;
p -> next = NULL; /*initializes pointer to null*/
return p;
}


/** Prints the nodes
* @param struct node_t *node
*/
//void print_node (struct node_t *node )
//{
// prints out the node and the address of the node
// print(*node, &node)
//}


/** Prints list
* @param struct node_t *head
*/
//void print_list (struct node_t *head)
//{
// print contents of list. both the doubles and the address.
// while (pointer is pointing to something, keep going through the list. )
// print(nodes in list, address of nodes )
//}


/** Inserts a node at the head
* @param struct node_t *head, the structure where head is.
* @param struct node_t *node, the structure where node is.
* @return head.
*/
struct node_t *insert_head(struct node_t *head, struct node_t *node)
{
node-> next = head;
head = node;
return head;
}


/** Inserts a node at the tail.
* @param struct node_t *tail, the structure where tail is.
* @param struct node_t *node, the structure where node is.
* @return head.
*/
struct node_t *insert_tail(struct node_t *tail, struct node_t *node)
{

struct node_t *p = head;
if (head == NULL)
head = node;
return head;
while (p->next ! = NULL)
p = p-> next;
p-> next = node;
return head;
}


/** Inserts a node at the middle.
* @param struct node_t *head, the structure where head is.
* @param struct node_t *node, the structure where node is.
* @param int pos, the position of the current node.
* @return head.
*/
struct node_t * insert_middle (struct node_t *head , struct node_t *node , int pos);
{

int pos = 1;
struct node_t *p = head;
*head = pos, pos = 1;
if (pos > SIZE )
p-> next = node;
else if (pos < 1)
printf("Error\n");
return head;
}


/** Counts the number of nodes
* @param struct node_t *head, the structure where head is.
* @return count.
*/
int count_nodes (struct node_t * head );
{

struct node_t *p = head;
while(p->next ! = NULL)
return count;
if (head == NULL)
count = 0;
return count;
}

/** Deletes nodes
* @param struct node_t *head, the structure where head is.
* @param double n, a number n.
* @return head.
*/
struct node_t * delete_node (struct node_t *head , double n);
{

if (node == head)
free head;
p->next = head; //head now points to the pointer that was next after the node that was deleted

else if (node == middle)
free node;
p = p->next //pointer before node that was deleted now points to node that was after deleted node

else if (node == tail)
free p = p->next = node; //free the last node that holds data. The node before it now points to NULL
p->next = NULL;

else printf("Error\n")
return head;
}


/** Deletes list
* @param struct node_t *head, the structure where head is.
* @return head.
*/
void delete_list (struct node_t * head );
{
while((p->next ! = NULL)
free head; //This will lose all memory after head.
head == NULL;
if (head == NULL)
return 0;
return 0;
}

最佳答案

那是链接器失败的时候。

您的文件顶部的前向声明签名可能与下面的实现不匹配。链接器找不到其中一个函数。

关于c - 我一直收到 collect2 : error: ld returned 1 exit status,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19848533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com