Hello Everyone,
I couldn't whip up a reasonable title for this post, so I just used the most broad title.
Currently, I am writing a class for a singly linked list, which by all means, is pretty straightforward. I am going to post code first, then explain.
In my abstract class, I have this function prototype (there are others, but for this purpose there are no reasons to show more)
Code:
// Get AND DELETE the first element of the list, placing it into the// return variable "value". If the list is empty, return false, otherwise
// return true.
virtual bool getfirst(Elem &returnvalue) = 0;
Elem is the name of the "typename or class" of the template. It holds ints/doubles. The functionality is
In my main, I have this to test my function. Below, you see the variable value. It is simply just an int with a default value (i.e. int value; )
Code:
// Print the list by repeatedly getting the first value cout << "Using getfirst():" << endl;
while (mylist.getfirst(value))
cout << value << ", ";
cout << endl;
And finally, here is my implementation of the getfirst function
Code:
template <class Elem>
bool LinkedSortedList<Elem>::getfirst( Elem &returnvalue )
{
LinkedNode<Elem>* tp = head; //temp pointer, starts at front of list
if( tp != NULL ) //if list is not empty
{
returnvalue = tp->value; //set the first item to returnvalue
head = tp->next; //update head pointer
delete tp; //delete node
count--;
return true;
}
else
{
return false;
}
}
Now, while running the test in main, this is supposed to return true, which it does, but it does not "return" the "return value".
I am passing the value by reference (address), so yes, I should be able to change the value permanently.
Also, I've tried using returnvalue = &tp->value but I get a compiler error : error C2440: '=' : cannot convert from 'int *' to 'int'
error C2440: '=' : cannot convert from 'double *' to 'double'
And finally, since this is a template, I instantiated two lists at the bottom of my implementation file. (e.g. LinkedSortedList<int> IntNeverUsed; )
mrJT