简体   繁体   English

链表中的链表-节点无法初始化

[英]linked list inside a linked list - node can't initialize

Im working on a c++ project for my data structures class, using nested linked lists. 我使用嵌套的链表为我的数据结构类在c ++项目中工作。

I am supposed to write a program that reads student names from a file, and creates a linked list out of them. 我应该写一个程序来从文件中读取学生姓名,并从中创建一个链接列表。 Each entry in the linked list should have the student's name, a pointer to the next student, and a pointer to a linked list of grades for that student. 链接列表中的每个条目都应具有该学生的姓名,指向下一个学生的指针以及指向该学生的成绩的链接列表的指针。 This program allows teachers to input grades for each student. 该程序允许老师为每个学生输入成绩。

As for now, Im worried about finding the right way to get started. 至于现在,我担心寻找正确的上手方法。

The book provided a diagram which shows an example of how the data structure should be: diagram 书提供了一种示意图,它表示的数据结构应如何一个例子:

struct Grade{
 float score;
};

struct GradeNode{
 Grade grade_ptr; //points to Grade above 
 GradeNode *next_gnode; //points to next Grade NODE
};

struct StudentNode {
 string name; //holds Student name 
 GradeNode *grades;  //points to linked list of grades for this student
 StudentNode *next_snode; //points to next student 
};

StudentNode* head = nullptr; //head of student linked list (set to nullptr)

I believe how I have this layed out makes sense but when I run code below: 我相信我如何进行此布局是有道理的,但是当我在下面运行代码时:

void appendNode(string n){

StudentNode *node;   //new node

node = new StudentNode;
node->name = n;
node->next_snode = nullptr;

//i just want to print out this node to see if value is initialized correctly
cout<<node->name<<endl; //this student's name prints out 

};

It returns an error saying "linker command failed". 它返回一个错误,指出“链接器命令失败”。 I feel like im initializing the node wrong but dont know how to fix it. 我觉得自己初始化节点错误,但不知道如何解决。

If someone has tips on how to find a solution, or a different approach for this task using linked lists, i would really appreciate it. 如果有人对如何使用链表找到解决方案或使用此任务的其他方法有所提示,我将不胜感激。

here is photo of error log 这是错误日志的照片 在此处输入图片说明

From image it appears you have declared and used a function buildList(std::string) (in main ) but never provide a definition to this function. 从图片看来,您已经声明并使用了一个函数buildList(std::string) (在main ),但从未为该函数提供定义。

Solution: Give a body to buildList function. 解决方案:为body提供buildList函数。

The error occurs when a function has been used in main without providing the function definition. 在main中使用功能而不提供功能定义时会发生错误。 Define the function before usage. 使用前定义功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM