passing classes as parameters

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.

1
2
3
	studentList student;

	student.push(252875, "Jerry", "UTPA");


What I thought would work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)
			return true;
		else
			return false;
	};

	//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"


What exactly am I doing wrong?
The error message is telling you exactly what's wrong. You've defined studentList::push as follows:

void push(student s)

That takes one argument, a student object.

You're attempting to call it with three arguments:

student.push(252875, "Jerry", "UTPA");

You need to pass a student object into push, not an int and two strings.
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.

I'm guessing you didn't write this code?
Try:

1
2
3
4
5
6
7
8
student MyStudent;

student.id = 252875;
student.name = "Jerry";
student.university = "UTPA";

studentList MyList;
MyList.push(MyStudent);
I tried fatirishman53's code and it seems to accept it but it just tells this.

Error	1	error LNK2019: unresolved external symbol "public: __thiscall studentList::~studentList(void)" (??1studentList@@QAE@XZ) referenced in function _main	Error
	2	error LNK1120: 1 unresolved externals

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.
fatirishman's code should be

1
2
3
4
5
6
7
8
student MyStudent;

MyStudent.id = 252875;
MyStudent.name = "Jerry";
MyStudent.university = "UTPA";

studentList MyList;
MyList.push(MyStudent);


Rookie mistake :P
Ok, i see what I did wrong. How would I get it to display something though? Would it be something like head->data?
DOH! I knew I was going to fumble something ;). I'm so terrible at proof reading my own stuff!
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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);
};
1
2
3
const int TABLE_SIZE = 7;

speedTable myTable(TABLE_SIZE);

Topic archived. No new replies allowed.