Milton Pauta
Milton Pauta

Reputation: 41

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.

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 enter image description here

Upvotes: 0

Views: 243

Answers (2)

Akash C.A
Akash C.A

Reputation: 330

The error occurs when a function has been used in main without providing the function definition. Define the function before usage.

Upvotes: 0

Mohit Jain
Mohit Jain

Reputation: 30489

From image it appears you have declared and used a function buildList(std::string) (in main) but never provide a definition to this function.

Solution: Give a body to buildList function.

Upvotes: 2

Related Questions