?
Solved

Pass in a struct to a function

Posted on 2005-03-26
4
Medium Priority
?
209 Views
Last Modified: 2011-09-20
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
Comment
Question by:ispcorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 13637879
You need to call it with a ItemType, which I dont see the declaration for.


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

0
 
LVL 15

Expert Comment

by:efn
ID: 13637909
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13638015
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
 
LVL 15

Accepted Solution

by:
efn earned 2000 total points
ID: 13638077
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 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