Link to home
Start Free TrialLog in
Avatar of ispcorp
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\studentactionlist.cpp In member function `StudentActionList::StudentNode*  StudentActionList::getStudentByStudentNumber(char*)':
19 C:\Project1\studentactionlist.cpp syntax error before `->' token
29 C:\Project1\studentactionlist.cpp cannot convert `student' to ` StudentActionList::StudentNode*' in return

I'll list the objects individually in each comment...
Avatar of ispcorp
ispcorp

ASKER

#include "studentactionlist.h"
#include<cstddef>
#include<cassert>

StudentActionList::StudentActionList():StudentList()
{}
StudentActionList::~StudentActionList()
{}
StudentActionList::StudentNode *StudentActionList::getStudentByStudentNumber(char *paramStudentNumber)
{
    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->getStudentNumber(),paramStudentNumber))
            {
               break;
            }        
        }
        return *objStudent;
    }
}
Avatar of ispcorp

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(ListIndexOutOfRangeException);
        void modifyStudentYear(char *paramStudentYear) const
             throw(ListIndexOutOfRangeException);
        void modifyCredits(int paramCredits) const
             throw(ListIndexOutOfRangeException);
        void modifyGPA(double paramGPA) const
             throw(ListIndexOutOfRangeException);    
    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
Avatar of ispcorp

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)colStudents.get(i);---->This is where I would get them item from the vector, I wish to do the samething from the pointer.
}
Avatar of ispcorp

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::StudentActionList():StudentList()
{}
StudentActionList::~StudentActionList()
{}
StudentActionList::StudentNode *StudentActionList::getStudentByStudentNumber(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.getStudentNumber(),paramStudentNumber))
            {
               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
try this:
StudentNode *objStudent = head
 for (int i=0; i < itSize; ++i)
        {
  objStudent->=cur->item;
objStudent->cur=cur->next;
       
            if (strcmp(objStudent.getStudentNumber(),paramStudentNumber))
            {
               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.getStudentNumber(),paramStudentNumber))
            {
               break;
            }        
        }
hopw this help.
Avatar of ispcorp

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.
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
else
    {
        StudentNode *objStudent = head;
        for (int i=0; i < itSize; ++i)
        {
           // StudentNode *cur=cur->next;
            //objStudent=*cur->item;
            if (strcmp(objStudent.getStudentNumber(),paramStudentNumber))
            {
               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->getStudentNumber(),paramStudentNumber))
            {
               break;
            }        
objStudent =objStudent ->next;
        }
        return objStudent;
    }