c++ - 创建链表插入功能但代码没有运行

标签 c++ insert linked-list

无法识别在链表类中创建链表插入函数时出现的错误,编译器给出了这个“错误:‘(’标记之前的声明中的合格 ID 但似乎所有括号都正确放置。

    #include <iostream>

using namespace std;
    void additem();
    void deleteitem();
    void searchitem(int x);

struct student{
int data;
int * next;
};

   student * head;
   student * curr;

int main()
{
   int x;
   cout << "To add item type 1" << endl;
   cin >> x;

   switch(x)
   {
     case 1:
     additem();
   }

    return 0;
}


void additem()
{
    student * temp;

    if(head == NULL)
    {
        temp = new student;
        head = temp;
        curr = temp;
        temp->next = NULL;
        cout << "Enter data" << endl;
        cin >> temp->data << endl;
    }

    else if(head != NULL)
    {
         temp = new student;
         curr->next = temp;
         curr = temp;
         temp->next = NULL;
         cout << "Enter data" << endl;
         cin >> temp->data ;
    }
    else{
        break;
    }

}

最佳答案

您在 main 中声明了一个类和方法。这是不允许的(嵌套函数)。 linkedlist::additem需要在main之前定义。

关于c++ - 创建链表插入功能但代码没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39714628/

相关文章:

c++ - 重载 > 运算符在应该返回 false 时返回 true

c++ - 在 C++ 中查找队列的重复状态(顺序相同的元素)

c++ - 存储和检索这个的最佳方式..?

c - 为什么 BSD 在链表条目中使用双指针?

mysql - 如何抑制mysql中的插入

linux - SED:插入一个带有特殊字符的字符串

C 程序 - 从 LinkedList 中删除重复项

c++ - 使用智能指针作为 "non-owning references"有什么优缺点?

C++线程问题

带有选择和硬编码值的 SQL 插入