桑稚爱写代码 2022-03-28 15:21 采纳率: 25%
浏览 75
已结题

关于自定义单链表实现Student类型数据存放的问题,主要是结点的构造不太明白!(语言-c++)

实验要求: main 函数中利用单向链表类 SingleLinkedList, 定义和初始化一个 Student 类型的线性链表,在表中添加(push_back)和插入(insert)新的元素。

结点的构造
template<typename T>
class SLNode
{
public:
    T _data;
    SLNode<T>* next; 
    
    SLNode()
    {
        this->_data = NULL;
        this-> next = NULL;
    }
};
链表的实现
template<typename T>
class SingleLinkedList
{
private:
    SLNode<T>* _head;
    int _count;

public:

    SingleLinkedList()
    {
        SLNode<T>* _head = NULL;
        this->_count = NULL;
    }

    void push_back(SingleLinkedList* L, T data)
    {
        SLNode<T>* new_node = new SLNode();
        new_node->_data = data;
        new_node->next = NULL;

        SLNode<T>* last_node = L->_head;
        for (int i = 0; i < _count; i++)
        {
            last_node = last_node->next;
        }
        new_node->next = last_node->next;
        last_node->next = new_node;
    }
    void insert(SingleLinkedList* L, int pos, T data)
    {
        if (L == NULL)
        {
            pos = 0;
        }
        SLNode<T>* new_node = new SLNode();
        new_node->_data = data;
        new_node->next = NULL;

        SLNode<T>* pos_node = L->_head;
        for (int i = 0; i < pos; i++)
            pos_node = pos_node->next;
        new_node->next = pos_node->next;
        pos_node->next = new_node;
        L->_count++;
    }

    void show(SingleLinkedList* L)
    {
        if (L->_count == 0)
            cout << "该链表中没有数据" << endl;
        else {
            SLNode<T>* pcurrent = L->_head->next;
            while (pcurrent != NULL)
            {
                cout << pcurrent->_data << endl;
                pcurrent = pcurrent->next;
            }
            cout << endl;
        }
    }

};

测试函数
int main()
{
    
    Student s1 = { 001,"桑桑",18.0f,88.0l };
    Student s2 = { 002,"稚稚",8.0f,77.0l };
    Student s3 = { 000,"渊渊",25.0f,99.0l };
    SingleLinkedList<Student> L1;
        cout << s1 << endl;
    L1.push_back(&L1,s1);
    L1.push_back(&L1,s2);
    cout << s1 << endl;
    L1.insert(&L1, 0, s3);
    L1.show(&L1);
}

Student类
class Student
{
public:
    Student(int id, string name, float age, double score);
    ~Student();
    friend ostream& operator<<(ostream&os, const Student&s)
    {
       os << "学号:" << s._id << "  姓名:" << s._name << "  年龄" << s._age << "  成绩:" << s._score << endl;
            return os;
        
    }
    string str()
    {
        cout << "内部输出:" << endl;
        cout << "学号:" << _id << "  姓名:" << _name << "  年龄" << _age << "  成绩:" << _score << endl;
        return "\n";
    };
    Student& operator =(const Student&x)
    {
        this->_id = x._id;
        this->_age = x._age;
        this->_score = x._score;
        this->_name = x._name;
        
    
    }
private:
    int _id;
    string _name;
    float _age;
    double _score;

};

Student::Student(int id, string name, float age, double score)
{
    _id = id;
    _name = name;
    _age = age;
    _score = score;
}

Student::~Student()
{
}

报错
已启动生成…
1>------ 已启动生成: 项目: ex4p4, 配置: Debug x64 ------
1>p4.cpp
1>C:\Users\86150\source\repos\ex3\dsa\SingledLinkedList.h(83,1): error C2514: “SLNode”: 无法构建类模板
1>C:\Users\86150\source\repos\ex3\dsa\SingledLinkedList.h(7): message : 参见“SLNode”的声明
1>C:\Users\86150\source\repos\ex3\dsa\SingledLinkedList.h(83): message : 查看对正在编译的 类 模板 实例化“SingleLinkedList<T>”的引用
1>已完成生成项目“ex4p4.vcxproj”的操作 - 失败。
========== 生成: 成功 0 个,失败 1 个,最新 1 个,跳过 0 个 ==========

错误    C2514    “SLNode”: 无法构建类模板    ex4p4    C:\Users\86150\source\repos\ex3\dsa\SingledLinkedList.h    83    //83是最后一行,有点迷惑
希望可以详细指出我的错误原因,以及如何改正
  • 写回答

3条回答 默认 最新

  • 关注

    img

    代码修改如下:

    #include <iostream>
    using namespace std;
    
    template<typename T>
    class SingleLinkedList
    {
    private:
        class SLNode
        {
        public:
            T _data;
            SLNode* next;
    
            SLNode()
            {
                //this->_data = 0;
                this->next = NULL;
            }
        };
        SLNode* _head;
        int _count;
    
    public:
    
        SingleLinkedList()
        {
            _head =  new SLNode();
            _head->next = NULL;
            //SLNode<T>* _head = NULL;
            this->_count = 0;
        }
    
        void push_back( T data)
        {
            SLNode* new_node = new SLNode();
            new_node->_data = data;
            new_node->next = NULL;
    
            SLNode* last_node = _head;
            for (int i = 0; i < _count; i++)
            {
                last_node = last_node->next;
            }
            new_node->next = last_node->next;
            last_node->next = new_node;
        }
        void insert(int pos, T data)
        {
            if (_head == NULL)
            {
                pos = 0;
            }
            SLNode* new_node = new SLNode();
            new_node->_data = data;
            new_node->next = NULL;
    
            SLNode* pos_node = _head;
            for (int i = 0; i < pos; i++)
                pos_node = pos_node->next;
            new_node->next = pos_node->next;
            pos_node->next = new_node;
            _count++;
        }
    
        void show()
        {
            if (_count == 0)
                cout << "该链表中没有数据" << endl;
            else {
                SLNode* pcurrent = _head->next;
                while (pcurrent != NULL)
                {
                    cout << pcurrent->_data << endl;
                    pcurrent = pcurrent->next;
                }
                cout << endl;
            }
        }
    
    };
    
    class Student
    {
    public:
        Student();
        Student(int id, string name, float age, double score);
        ~Student();
        friend ostream& operator<<(ostream& os, const Student& s)
        {
            os << "学号:" << s._id << "  姓名:" << s._name << "  年龄" << s._age << "  成绩:" << s._score << endl;
            return os;
    
        }
        string str()
        {
            cout << "内部输出:" << endl;
            cout << "学号:" << _id << "  姓名:" << _name << "  年龄" << _age << "  成绩:" << _score << endl;
            return "\n";
        };
        Student& operator =(const Student& x)
        {
            this->_id = x._id;
            this->_age = x._age;
            this->_score = x._score;
            this->_name = x._name;
            return *this;
    
        }
    private:
        int _id;
        string _name;
        float _age;
        double _score;
    
    };
    Student::Student()
    {
        _id = 0;
        _name = "";
        _age = 0;
        _score = 0;
    }
    Student::Student(int id, string name, float age, double score)
    {
        _id = id;
        _name = name;
        _age = age;
        _score = score;
    }
    
    Student::~Student()
    {
    }
    
    
    
    
    
    int main()
    {
    
        Student s1 = { 001,"桑桑",18.0f,88.0l };
        Student s2 = { 002,"稚稚",8.0f,77.0l };
        Student s3 = { 000,"渊渊",25.0f,99.0l };
        SingleLinkedList<Student> L1;
        cout << s1 << endl;
        L1.push_back(s1);
        L1.push_back(s2);
        cout << s1 << endl;
        L1.insert(0, s3);
        L1.show();
        return 0;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 4月5日
  • 已采纳回答 3月28日
  • 修改了问题 3月28日
  • 创建了问题 3月28日

悬赏问题

  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题