?
Solved

Return an object from an ADT list

Posted on 2005-03-24
12
Medium Priority
?
199 Views
Last Modified: 2010-04-01
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...
0
Comment
Question by:ispcorp
  • 5
  • 4
  • 3
12 Comments
 
LVL 2

Author Comment

by:ispcorp
ID: 13627191
#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;
    }
}
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13627193
#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;
};
0
 
LVL 15

Expert Comment

by:efn
ID: 13627443
>  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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:ispcorp
ID: 13627468
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.
}
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13627490
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;
    }
}



0
 
LVL 15

Expert Comment

by:efn
ID: 13627870
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
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13627994
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.
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13628016
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.
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13631774
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.
0
 
LVL 15

Accepted Solution

by:
efn earned 2000 total points
ID: 13632433
objStudent is a pointer to a StudentNode.  (You also have another objStudent in a different scope that is an ItemType.  This one is not used, so I suggest you get rid of it to reduce the potential for confusion.)

objStudent->item is an ItemType.  This is another name for a student.

If a student has a getStudentNumber member function, you should be able to call objStudent->item.getStudentNumber().
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13632434
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;
    }
0
 
LVL 5

Expert Comment

by:beryl666
ID: 13632441
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;
    }
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question