Trying to capture a parameter from a void function.

Should be an easy to answer question.

Page 1 of 1

3 Replies - 719 Views - Last Post: 11 November 2008 - 07:35 AM Rate Topic: -----

#1 haynest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-November 08

Trying to capture a parameter from a void function.

Post icon  Posted 09 November 2008 - 12:53 PM

Hello everyone! I have been using this website for some time now to get help with my programming assignments, but I am really stuck on this one.

I am trying to get a parameter from a void function. Part of the assignment is that I cannot change the function from void. I am basically trying to perform a binary search using a retrieve function from a header file.

Here is my some of the code of binary search function. If needed i'll post everything, but I think the answer is pretty simple and I am just too much of a newbie to see it.
List alist;

if (newUser == aList.retrieve(mid(int), dataItem(struct), success(boolean))) 

index = mid;  // value found at anArray[mid]

else if (newUser < aList.retrieve(mid, dataItem, success)) 




As you can see aList.retrieve(mid, dataItem, success) is my problem.

retrieve is a void function. But I need to use the dataItem parameter to capture an item from the list in question in one function call, and then, in a separate statement, compare that item. Like I am trying to do in my nonworking 1st if statement. And by not working I mean compiler errors.

un2.cpp: In function `int binarySearch(std::string, int, int, int, bool)':
un2.cpp:134: error: no matching function for call to `List::retrieve(int&, std::string&, bool&)'
ListA.h:64: note: candidates are: void List::retrieve(int, ListItemType&, bool&) const
un2.cpp:136: error: no matching function for call to `List::retrieve(int&, std::string&, bool&)'
ListA.h:64: note: candidates are: void List::retrieve(int, ListItemType&, bool&) const

I really hope somebody can answer my question. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to capture a parameter from a void function.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Trying to capture a parameter from a void function.

Posted 09 November 2008 - 01:11 PM

Well first of all, I have to assume you have your own custom List object because List in C++ doesn't come with a retrieve function. Second if the function is void you can't use it directly in the comparison of your if statement... if (newUser == aList.retrieve(mid(int), dataItem(struct), success(boolean))) . Instead you are going to call aList.retrieve first, so that dataItem is set and THEN use dataItem in your comparison to newUser.

Again, you are not going to be able to use it directly in the elseif statement. Call the function before any comparisons, this will then set dataItem for you. Then use dataItem in the comparisons. Because if the function is void, your call to it is going to return NOTHING and you can't use that in the comparison if statements.

I hope I am making myself clear on this. :)
Was This Post Helpful? 1
  • +
  • -

#3 haynest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-November 08

Re: Trying to capture a parameter from a void function.

Posted 09 November 2008 - 02:39 PM

Yes, I do. Sorry I should have specified.

Yes, you were very clear and I appreciate it very much. Here is my revised code for future people looking for a similar answer.

aList.retrieve(mid, dataItem, success); //call the retrieve function and return struct dataItem

tmpUserName = dataItem.userName;	//assign the value of dataItem.userName to temp variable

if (newUser == tmpUserName) //now I can compare



I can finally compile my code OK, so I hope the code will also work correctly. But that will be for another topic. Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 haynest  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 09-November 08

Re: Trying to capture a parameter from a void function.

Posted 11 November 2008 - 07:35 AM

Oh my!

My code compiled ok, but it does not work properly.

I called the function.
Then attempted to assign dataItem.userName to tmpUserName for comparison.
In my cout statements listed below tmpUserName does not display on the screen, it is empty.
And yes aList is a custom list object.

//Some code in a function called binarysearch from un2.cpp
aList.retrieve(mid, dataItem, success); //call the retrieve function and return struct dataItem
					tmpUserName = dataItem.userName;	//assign the value of dataItem.userName to temp variable
					cout << tmpUserName << "	 tmpUserName" << endl;
					cout << newUser << "	 newUser" << endl;
					cout << dataItem.userName << "	 dataItem.userName" << endl;



//retrieve function that is in ListA.cpp
void List::retrieve(int index, ListItemType& dataItem,
					bool& success) const
{
   success = (index >= 1) && (index <= size);
   if (success)
	  dataItem = items[translate(index)];
}  // end retrieve



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1