I was wondering, how would I pass this parameter and how/why is it not working this way? I've tried many different methods to this and I can't quite seem to figure it out. I was sick when I was assigned this so I missed the lecture on how this works and I was hoping someone here could help me. Here is what i am talking about.
class student
{
public:
int id; //student ID number
string name; //student’s name
string university; //student’ university
};
//student list is a doubly linked list of students.
class studentList
{
private:
class node
{
public:
student data;
node * next;
node * prev;
};
node * head;
public:
studentList()
{
head = NULL;
}
//be sure to free all dynamically allocated memory!
~studentList();
//return true if the list is empty, false if not
bool empty()
{
if(head == NULL)
returntrue;
elsereturnfalse;
};
//insert student s into the front of the linked list
void push(student s)
{
node * nodeptr;
nodeptr = new node();
nodeptr->data = s;
nodeptr->next = head;
head = nodeptr;
nodeptr->prev = head;
if (nodeptr->next != NULL)
nodeptr->next->prev = nodeptr;
};
//remove and return the student at the front of the list
student pop()
{
node * nodeptr;
student y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y.id = nodeptr->data.id;
delete nodeptr;
return y;
};
};
My header file.
I am honestly not sure where to start here. I would assume that it would know what to do with the varibles but it doesn't seem to want to accept them. It gives me
Error 1 error C2660: 'studentList::push' : function does not take 3 arguments
2 IntelliSense: no suitable constructor exists to convert from "int" to "student"
Well I know what you mean when I look at it and I thank you for the explaination but what I want to know is this. What do I pass it and how? I mean, I want to input the information that I need like the ID number, Name, and the name of the University but I am not exactly sure how to. I figured this was what it was asking for and I just needed to specify or something but I guess not. What can I do to make it where I can pass in the parameters?
Well I know what you mean when I look at it and I thank you for the explaination but what I want to know is this. What do I pass it and how?
You pass it an object of type student. The syntax of this is identical to passing an argument of any other type.
I mean, I want to input the information that I need like the ID number, Name, and the name of the University but I am not exactly sure how to.
I think you're getting confused about what it is push is doing. push isn't creating a student object. It's taking an existing student object, and pushing it onto the list.
So before you can push the student object onto the list, you need to create the student object, and set the values of the data it contains.
The linker is telling you what the problem is: you haven't defined studentList::~studentList(void) - i.e. the destructor. You've declared it, but not defined it.
Thank you guys for that bit of information. What would I do in the case of if I want to pass an item to the constructor since I would like to implement a hashtable to help sort out the students further in order to cut down on the number of comparisons that the user has to do in order to find the student. One thing I have is this template but I am not exactly sure how to pass size to speedtable.
class speedTable
{
private:
studentList * table; //an array of studentLists
int tableSize; //size of the array
public:
//constructor which will allocate a new array of studentLists of size ‘size’
speedTable(int size);
//be sure to free all dynamically allocated memory!
~speedTable();
//add student s to data structure
void add(student s);
//remove student with given id from data structure
void remove(int id);
//locate and return the student with given id from the data structure
student getStudent(int id);
//Do this method last. Create a new array of size newsize and rehash
//all items in the old table into the new table.
//Free (delete) the old array and point the table
//variable at the new array.
void resize(int newsize);
};