I've got a program, that stores sets of classes and structs in lists.
It does the following:
- Passes an input (an
int
), an iterator, a list and a pointer by reference into the function check()
- Iterates the list until it finds a match between the iterator's data and the input
- Sets the pointer as the iterator's position
- Returns true or false, depending on whether a match was found or not.
My problem is, when I call the function display()
from within the function check, whether it be from it->display()
, or Ptr->display()
, it works fine. But when it's passed back out by reference, and I try to display it. It prints garbage.
//it is the iterator, l is the list, Ptr is the passed pointer
template<class T, class T2, class P>
bool Inspection::check(int input, T it, T2 l, P * &Ptr)
{
for(it = l.begin(); it != l.end(); ++it){ //Iterates through list using iterator
if (it->checkExists(input)){ //if input == iterator class's data
Ptr = &*it;
//Display data - ERROR CHECKING//
it->display();
Ptr->display();
return true;
}
}
return false;
}
checkExists
is a function that compares to the private data in the class it's iterating, Such as
bool Property::checkExists(int input)
{
if (input == ID)
return true;
return false;
}
display
is also straightforward
void Property::display()
{
//Prints out property info
cout << ID << ";" << address << ";" << landTypes[type] << ";" << price << endl;
}
A standard call is (p
is a list of the Property
classes that I've called earlier in the program)
int input;
Property * temp; //Pointer to a class temp
list<Property>::iterator pIT;
cin >> input;
while(!check(input, pIT, p, temp)){
...
}
temp->display();
A typical output would be (First two are calls within the function and correct, the third is the temp->display();
call from outside the function.
1001;5/10 Northfields Ave, North Wollongong, NSW 2500;Townhouse;280000
1001;5/10 Northfields Ave, North Wollongong, NSW 2500;Townhouse;280000
13;�������\314���@�ve, North Wollongong, NSW 2500;Townhouse;280000
EDIT: Sorry I linked the wrong display function(). Edited code to update