You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++多变量链表待办项目addTask函数类型转换错误求助

链表待办事项列表addTask函数类型不匹配错误分析与修复

问题描述

我正在开发一个基于链表的待办事项列表项目,在头文件的addTask()函数中遇到两处类型不匹配错误:

错误代码片段:

if (head != NULL)
{
    current = head;
    while (current->link != NULL)
    {
        current = current->link; // 错误位置
    }
    current->link = n; // 另一错误位置
}

报错信息:

  • '=' cannot convert from 'list*' to 'list::taskPTR'
  • '=' cannot convert from 'list::task*' to 'list*'

完整头文件代码:

#ifndef linkedList
#define linkedList

#include <iostream>

using namespace std;

class list
{
private:
    struct task
    {
        string taskDue;
        string taskDesc;
        string taskCourse;
        string day;
        string mon;
        string year;
        list* link;
    };

    typedef struct task* taskPtr;

    taskPtr head;
    taskPtr current;
    taskPtr temp;

public:

    list();
    ~list();
    void printByDue();
    void printByCourse();
    void printByStatus();
    void addTask(string addTaskDue, string addTaskDesc, string addTaskCourse, string addDay, string addMon, string addYear);
    void delTask(string delTaskDue, string delTaskDesc, string delTaskCourse, string delDay, string delMon, string delYear);

};


list::list() //constructor
{
    head = NULL;
    current = NULL;
    temp = NULL;
}

void list::addTask(string addTaskDue, string addTaskDesc, string addTaskCourse, string addDay, string addMon, string addYear)
{
    struct task* n = new task;
    n->link = NULL;

    n->taskDue = addTaskDue;
    n->taskDesc = addTaskDesc;
    n->taskCourse = addTaskCourse;
    n->day = addDay;
    n->mon = addMon;
    n->year = addYear;

    if (head != NULL)
    {
        current = head;
        while (current->link != NULL)
        {
            current = current->link;
        }
        current->link = n;
    }
    else
    {
        head = n;
    }
}

void menu()
{
    char option;
    cout << " Welcom to the ToDo List Program by ACGR";
    cout << "\n" << endl;
    cout << "What would you like to do? \n" << endl;
    cout << "\t[a]dd a new task" << endl;
    cout << "\t[c]omplete a task" << endl;
    cout << "\t[d]isplay tasks" << endl;
    cout << "\t[q]uit" << endl;

    cout << "\nEnter choice here: ";
}
#endif // !linkedList.h

错误原因

核心问题是task结构体中link成员的类型定义错误:

  • 链表节点的指针应该指向同类型的节点,也就是task*,但你错误地将其声明为list*(指向整个链表类的指针)。

对应两个错误的具体解释:

  1. current = current->link:current是taskPtr(等价于task*),但current->link是list*,两种指针类型不兼容,无法直接赋值。
  2. current->link = n:current->link是list*,而n是task*,类型不匹配导致赋值失败。

修复方法

将task结构体中的list* link;修改为task* link;即可解决所有类型不匹配问题。

修改后的task结构体代码:

struct task
{
    string taskDue;
    string taskDesc;
    string taskCourse;
    string day;
    string mon;
    string year;
    task* link; // 修正为task*类型
};

修改后,addTask()函数中的原有逻辑无需改动,类型匹配后错误会自动消失。

内容的提问来源于stack exchange,提问作者Joaquin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:01:31