Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Pass in a struct to a function

Hello,
How would I go about passing a struct, which looks like the following...

typedef student ItemStudentType; //student is a class
struct StudentNode
        {
               ItemStudentType item;
               StudentNode *next;
        };

into a function that accepts a type of the same class but different name...if this doesn't make any sense, here is the function where i'm trying to pass to...

void StudentList::insert(int index,ItemType newItem) throw (ListIndexOutOfRangeException)
{
     int newLength = getLength() + 1;
     if ((index < 1) || (index > newLength))
        throw ListIndexOutOfRangeException("ListOutRangeException: insert index out of range");
     else
     {
         Node *newPtr = new Node;
         if (newPtr == NULL)
            throw ListException("ListException: insert cannot allocate memory");
         else if(size==MAX)
              throw ListException("ListException: maximum number of students have been reached");
         else
         {
             size = newLength;
             newPtr->item=newItem;
             if (index==1)
             {
                newPtr->next=head;
                head=newPtr;
             }
             else
             {
                 Node *prev = find(index-1);
                 newPtr->next=prev->next;
                 prev->next=newPtr;
             }
         }
     }
}
I call it by this statement insert(getLength(), *objStudentNode);

Object *objStudent is instantiated by this call...StudentNode *objStudent = new StudentNode();

I should note that the insert function is a method inside a base class, which I inherit from the class in which I'm calling it from.  The calling class has its own struct but with the same class.


0
ispcorp
Asked:
ispcorp
  • 2
1 Solution
 
grg99Commented:
You need to call it with a ItemType, which I dont see the declaration for.


do you mean:   insert(getLength(), objStudent.item );  ??

0
 
efnCommented:
If ItemType and ItemStudentType are supposed to be the same thing and objStudentNode is supposed to be the same as objStudent, you could write

insert(getLength(), objStudent->item)
0
 
ispcorpAuthor Commented:
Sorry for the late response...wife was getting on my case...

I'm getting this error when I follow your changes...

for the insert(getLength(),objStudent->item)
120 C:\Project1\studentactionlist.cpp passing `const StudentActionList' as `this'  argument of `void StudentList::insert(int, student)' discards qualifiers

and for the insert(getLength(),objStudent.item)
120 C:\Project1\studentactionlist.cpp request for member `item' in `objStudent',  which is of non-class type `StudentActionList::StudentNode*'

0
 
efnCommented:
The first error message says that you are trying to insert into a const StudentList, that is, one you told the compiler you didn't want to change.  Sometimes, you can fix errors like this by declaring the member function const, but logically, an insert function should change the contents of the object, so that wouldn't be appropriate here.  So you probably have to figure out how to deconst the StudentList into which you are trying to insert.  I can't be any more specific without seeing more code.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now