ispcorp
asked on
Return an object from an ADT list
Hello,
I have a linked list where I would like to store class objects within it. I have a function called getStudentByStudentNumber which accepts a string parameter and loops through the list and stops if it finds the number in a property of the object. I'm having trouble with it, and here are the errors I'm getting...
C:\Project1\studentactionl ist.cpp In member function `StudentActionList::Studen tNode* StudentActionList::getStud entByStude ntNumber(c har*)':
19 C:\Project1\studentactionl ist.cpp syntax error before `->' token
29 C:\Project1\studentactionl ist.cpp cannot convert `student' to ` StudentActionList::Student Node*' in return
I'll list the objects individually in each comment...
I have a linked list where I would like to store class objects within it. I have a function called getStudentByStudentNumber which accepts a string parameter and loops through the list and stops if it finds the number in a property of the object. I'm having trouble with it, and here are the errors I'm getting...
C:\Project1\studentactionl
19 C:\Project1\studentactionl
29 C:\Project1\studentactionl
I'll list the objects individually in each comment...
ASKER
#include "studentlist.h"
#include <iostream>
#include <list>
#include <string>
int itSize;
typedef student ItemType ;
class StudentActionList: public StudentList
{
public:
StudentActionList();
~StudentActionList();
struct StudentNode
{
ItemType item;
StudentNode *next;
};
StudentNode *getStudentByStudentNumber (char *paramStudentNumber);
void deleteStudent(ItemType *paramStudent) const
throw(ListIndexOutOfRangeE xception);
void modifyStudentYear(char *paramStudentYear) const
throw(ListIndexOutOfRangeE xception);
void modifyCredits(int paramCredits) const
throw(ListIndexOutOfRangeE xception);
void modifyGPA(double paramGPA) const
throw(ListIndexOutOfRangeE xception);
private:
StudentNode *head;
};
#include <iostream>
#include <list>
#include <string>
int itSize;
typedef student ItemType ;
class StudentActionList: public StudentList
{
public:
StudentActionList();
~StudentActionList();
struct StudentNode
{
ItemType item;
StudentNode *next;
};
StudentNode *getStudentByStudentNumber
void deleteStudent(ItemType *paramStudent) const
throw(ListIndexOutOfRangeE
void modifyStudentYear(char *paramStudentYear) const
throw(ListIndexOutOfRangeE
void modifyCredits(int paramCredits) const
throw(ListIndexOutOfRangeE
void modifyGPA(double paramGPA) const
throw(ListIndexOutOfRangeE
private:
StudentNode *head;
};
> ItemType *objStudent = ItemType->head;
If you are going to search through the list, it makes sense to start from the beginning. The elements of the list are StudentNodes and they are connected by pointers, so you want a pointer to a StudentNode, not a pointer to an ItemType. You can conveniently initialize such a pointer with the head member, because it is just such a pointer itself.
If you want the getStudentByStudentNumber function to return an object, it should return a student or an ItemType, not a pointer to a StudentNode. If p is a pointer to a StudentNode and you want to return the student from the node, you can return p->item.
--efn
If you are going to search through the list, it makes sense to start from the beginning. The elements of the list are StudentNodes and they are connected by pointers, so you want a pointer to a StudentNode, not a pointer to an ItemType. You can conveniently initialize such a pointer with the head member, because it is just such a pointer itself.
If you want the getStudentByStudentNumber function to return an object, it should return a student or an ItemType, not a pointer to a StudentNode. If p is a pointer to a StudentNode and you want to return the student from the node, you can return p->item.
--efn
ASKER
Ok, thanks, but how do I convert the pointer in the list to be an the student object. I know java, and if i would do it in java then I would do something like this...
Think of the vector as pointer...
Vector colStudents = new Vector();
student objStudent = null;
int itSize = colStudent.size();
for (int i=0; i < itSize; i++)
{
objStudent=(student)colStu dents.get( i);---->Th is is where I would get them item from the vector, I wish to do the samething from the pointer.
}
Think of the vector as pointer...
Vector colStudents = new Vector();
student objStudent = null;
int itSize = colStudent.size();
for (int i=0; i < itSize; i++)
{
objStudent=(student)colStu
}
ASKER
I changed the procedure to the following...And I only get one error, and its a problem with getting the property from the student class, because I don't know how to get the object from the pointer.
#include "studentactionlist.h"
#include<cstddef>
#include<cassert>
StudentActionList::Student ActionList ():Student List()
{}
StudentActionList::~Studen tActionLis t()
{}
StudentActionList::Student Node *StudentActionList::getStu dentByStud entNumber( char *paramStudentNumber)
{
int itSize = getLength();
ItemType objStudent;
if (itSize == 0)
{
return NULL;
}
else
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
StudentNode *cur=cur->next;
objStudent=*cur->item;
if (strcmp(objStudent.getStud entNumber( ),paramStu dentNumber ))
{
break;
}
}
return objStudent;
}
}
#include "studentactionlist.h"
#include<cstddef>
#include<cassert>
StudentActionList::Student
{}
StudentActionList::~Studen
{}
StudentActionList::Student
{
int itSize = getLength();
ItemType objStudent;
if (itSize == 0)
{
return NULL;
}
else
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
StudentNode *cur=cur->next;
objStudent=*cur->item;
if (strcmp(objStudent.getStud
{
break;
}
}
return objStudent;
}
}
I was taking your word for it that you want to return an object, but given that you are coming from Java, that may not be what you really want. You have a choice of two ways to go: you can return an object by value, or you can return a pointer to an object.
If you return an object by value, as suggested by the title of your question, the getStudentByStudentNumber function will return a student object that is a copy of one in its collection. If the user of this function just wants to be able to read from the object and throw it away, that can be OK, but if the function caller does something to the returned object, that will have no effect on the original student, still stored in the StudentActionList. Note that with this approach, you can't return a null reference--you have to return some kind of student object, even if it doesn't contain any interesting data. If you go this way, my comment above tells you how to return a copy of a student when you have a pointer to a StudentNode.
The other design approach is to have the StudentActionList container return a pointer to a student that it owns and contains. Then the function caller can read and change that object, not a copy. This also means that the function can return a null pointer to signify that there is nothing to retrieve. If you have a pointer p to a StudentNode, p->item is the student, so &(p->item) is the address of the student, which the function can return.
In neither case do you want the function to return a pointer to a StudentNode. In the first case, it should return a student, and in the second case, it should return a pointer to a student.
--efn
If you return an object by value, as suggested by the title of your question, the getStudentByStudentNumber function will return a student object that is a copy of one in its collection. If the user of this function just wants to be able to read from the object and throw it away, that can be OK, but if the function caller does something to the returned object, that will have no effect on the original student, still stored in the StudentActionList. Note that with this approach, you can't return a null reference--you have to return some kind of student object, even if it doesn't contain any interesting data. If you go this way, my comment above tells you how to return a copy of a student when you have a pointer to a StudentNode.
The other design approach is to have the StudentActionList container return a pointer to a student that it owns and contains. Then the function caller can read and change that object, not a copy. This also means that the function can return a null pointer to signify that there is nothing to retrieve. If you have a pointer p to a StudentNode, p->item is the student, so &(p->item) is the address of the student, which the function can return.
In neither case do you want the function to return a pointer to a StudentNode. In the first case, it should return a student, and in the second case, it should return a pointer to a student.
--efn
try this:
StudentNode *objStudent = head
for (int i=0; i < itSize; ++i)
{
objStudent->=cur->item;
objStudent->cur=cur->next;
if (strcmp(objStudent.getStud entNumber( ),paramStu dentNumber ))
{
break;
}
}
hopw this help.
StudentNode *objStudent = head
for (int i=0; i < itSize; ++i)
{
objStudent->=cur->item;
objStudent->cur=cur->next;
if (strcmp(objStudent.getStud
{
break;
}
}
hopw this help.
try this:
StudentNode *objStudent = head
for (int i=0; i < itSize; ++i)
{
objStudent->item=cur->item ;//sorry error here.
objStudent->cur=cur->next;
if (strcmp(objStudent.getStud entNumber( ),paramStu dentNumber ))
{
break;
}
}
hopw this help.
StudentNode *objStudent = head
for (int i=0; i < itSize; ++i)
{
objStudent->item=cur->item
objStudent->cur=cur->next;
if (strcmp(objStudent.getStud
{
break;
}
}
hopw this help.
ASKER
Ok, the problem still exists where I cannot get the getStudentNumber() property out of the objStudent. All its giving me is "item" and "next".
StudentNode is a struct, which looks like the following
typedef student ItemType;
struct StudentNode
{
ItemType item;
StudentNode *next;
};
The ItemType variable is instantiated with the student class, which contains these properties. I need the properties of the student class, which I expect the List will hold. Like a collection of some sort.
StudentNode is a struct, which looks like the following
typedef student ItemType;
struct StudentNode
{
ItemType item;
StudentNode *next;
};
The ItemType variable is instantiated with the student class, which contains these properties. I need the properties of the student class, which I expect the List will hold. Like a collection of some sort.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
else
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
// StudentNode *cur=cur->next;
//objStudent=*cur->item;
if (strcmp(objStudent.getStud entNumber( ),paramStu dentNumber ))
{
break;
}
objStudent =objStudent ->next;
}
return objStudent;
}
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
// StudentNode *cur=cur->next;
//objStudent=*cur->item;
if (strcmp(objStudent.getStud
{
break;
}
objStudent =objStudent ->next;
}
return objStudent;
}
ignore above .. try this
else
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
// StudentNode *cur=cur->next;
//objStudent=*cur->item;
if (strcmp(objStudent->getStu dentNumber (),paramSt udentNumbe r))
{
break;
}
objStudent =objStudent ->next;
}
return objStudent;
}
else
{
StudentNode *objStudent = head;
for (int i=0; i < itSize; ++i)
{
// StudentNode *cur=cur->next;
//objStudent=*cur->item;
if (strcmp(objStudent->getStu
{
break;
}
objStudent =objStudent ->next;
}
return objStudent;
}
ASKER
#include<cstddef>
#include<cassert>
StudentActionList::Student
{}
StudentActionList::~Studen
{}
StudentActionList::Student
{
int itSize = getLength();
if (itSize == 0)
{
return NULL;
}
else
{
ItemType *objStudent = ItemType->head;
for (int i=0; i < itSize; ++i)
{
StudentNode *cur=cur->next;
*objStudent=(ItemType&)cur
if (strcmp(objStudent->getStu
{
break;
}
}
return *objStudent;
}
}