I am having problem with initializing the linked list (*exam and *subject) through the constructor of Student class, i tried by creating two new linked lists (sub and exm) and assign their addresses to both pointers (subject and exam)(refer to student class constructor), however i had a problem where the both linked lists contains garbage values such as count in exam linked list = -853218796854 instead of getting 0 the codes is given below... Each classes in different header file.
class Exam {
public:
int trimester; //1 = jan, 5 = may and 10 = oct
int year;
int totalcredit;
double gpa;
int numOfSubjects; //for one trimester
Subject sub[6]; //list of subjects taken by student in a semester
Exam();
};
Exam::Exam(){
trimester = 0;
year = 0;
gpa = 0.0;
numOfSubjects = 0;
totalcredit = 0;
}
You are assigning your exam and subject pointers to local variables whose scope expires at the end of the constructor. You need to use new to create a new instance for subject and exam.
Put the following into your constructor:
1 2
subject = new DLL<Subject>;
exam = new DLL<Exam>;
And remove the sub and exm vars you are using now.
oh nice now it works , thanks, been working this thing for straight 12 hours tried your method before it has some syntax error ,was wondering why and stop using that, guess i type something wrong LOL , thanks again man!
after it works, i take a rest, after that now i check again i had another problem, for all students with exam linked list are point to same memory address, so when i set new updated student with added exam records for a particular student, all other students also get the exam records, i apologize for my English, maybe its hard to understand.
DLL<Student> aStudent;
inside first node of aStudent:
exam with memory address 0x00385130
inside second node of aStudent:
exam with memory address 0x00385130
..... all exam nodes in aStudent will have the same record , so what should i do to avoid that?
I actually tried, Student class contains 2 more sub linked list , which are exam and subject, for this piece of code i only add exam into student, and all other student can access to a particular student exam in linked list , hope you can understand.