我在主函数的左括号上不断收到错误,消息为obj\Debug\main.o||在函数"ZN11linked_listC1Ev"中:|

I keep getting an error on the opening bracket of my main function with the message objDebugmain.o||In function `ZN11linked_listC1Ev':|

本文关键字:函数 Debug obj main ZN11linked listC1Ev 错误 消息      更新时间:2023-10-16

我尝试过在Visual Studio和CodeBlocks中编译,我收到了不同的错误消息。几个小时以来,我一直在努力解决这个问题,非常感谢您的帮助。我只是想写一个简单的链表程序。

这是我的头文件:

#ifndef LINKED_LIST_H_INCLUDED
#define LINKED_LIST_H_INCLUDED
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;
/*
* Class Declaration
*/
class linked_list
{
public:
node* create_node(int);
void insert_begin();
void insert_last();
void insert_pos();
void delete_pos();
void delete_begin();
void delete_last();
void display();
linked_list()
{
start = NULL;
}
};

#endif // LINKED_LIST_H_INCLUDED

这是我的实现文件:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;
/*
* Create Node
*/
node *linked_list::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;
return temp;
}
}
/*
* Display all the elements of the linked list
*/
void linked_list::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
/*
* Inserting at the beginning of the list
*/
void linked_list::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
/*
* Inserting Node at the end of the list
*/
void linked_list::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last position"<<endl;
}
/*
* Insertion of node at the specified position
*/
void linked_list::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the position at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1  && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Position out of range"<<endl;
}
}

/*
* Deletion element at a given position
*/
void linked_list::delete_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
/*
* Deletion of element at the beginning of the list
*/
void linked_list::delete_begin()
{
struct node *temp, *p;
temp = start;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
else
{
p = start;
start = temp;
p=start->next;
delete temp;
}
cout<<"Element deleted at beginning"<<endl;
}
/*
* Deletion of element at the end of the list
*/
void linked_list::delete_last()
{
struct node *p, *s;
s = start;
while (s->next != NULL)
{
p = s;
s = s->next;
}
p->next = NULL;
delete s;
cout<<"Element deleted at last position"<<endl;
}

这是我的驱动程序文件:

#include <iostream>
#include<cstdio>
#include<cstdlib>
#include "linked_list.h"
using namespace std;

main()
{
int choice, nodes, element, position, i;
linked_list linlist;
start = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Linked List Homework Menu"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at the Beginning of the List"<<endl;
cout<<"2.Insert Node at the Last Position in the List"<<endl;
cout<<"3.Insert Node at Specified Position"<<endl;
cout<<"4.Delete Node at Specified Position"<<endl;
cout<<"5.Delete Node at the Last Position in the List"<<endl;
cout<<"6.Delete Node at the Beginning of the List"<<endl;
cout<<"7.Display Linked List"<<endl;
cout<<"8.Exit "<<endl;
cout<<"Please enter a selection : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at the Beginning: "<<endl;
linlist.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at the Last Position: "<<endl;
linlist.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a Specific Position:"<<endl;
linlist.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Deleting Node at a Specific Position: "<<endl;
linlist.delete_pos();
break;
case 5:
cout<<"Deleting Node at the Last Position: "<<endl;
linlist.delete_last();
break;
case 6:
cout<<"Deleting Node at the Beginning: "<<endl;
linlist.delete_begin();
break;
case 7:
cout<<"Display the linked list"<<endl;
linlist.display();
cout<<endl;
break;
case 8:
cout<<"Exiting Program... Goodbye!"<<endl;
exit(1);
break;
default:
cout<<"Option not viable"<<endl;
}
}
}

当编译器到达驱动程序文件中的第 10 行时,会遇到此问题。我将把Visual Studio和CodeBlocks的两个错误列表放在下面。我仍然在掌握C++所以提前感谢任何决定提供帮助的人!

错误日志 其他错误日志

第一个错误

struct node
{
int info;
struct node *next;
}*start;

您在 heaader 文件中创建了非常量变量。然后,此标头包含在两个源文件中。 最终,您在两个目标文件中有两个称为"start"的变量。这就是链接器抛出错误"启动的多重定义"的原因。

正如注释中提到的,"start"变量必须移动到类linked_list。

第二个错误

main()

应该是

int main()