Question

I've got a program, that stores sets of classes and structs in lists.

It does the following:

  1. Passes an input (an int), an iterator, a list and a pointer by reference into the function check()
  2. Iterates the list until it finds a match between the iterator's data and the input
  3. Sets the pointer as the iterator's position
  4. 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

Was it helpful?

Solution

Not withstanding the design issues pointed out by WhozCraig the issue with printing out garbage in the code you provided is as follows:

 template<class T, class T2, class P>
 bool Inspection::check(int input, T it, T2 l, P * &Ptr)
                                         ^^^^

You are passing l by value instead of by reference, so you are getting back a pointer to a temporary variable which won't exist when you dereference it outside of the method. If you modify the code as following it should start working for this particular issue although it really does need a redesign:

template<class T, class T2, class P>
bool Inspection::check(int input, T it, T2 &l, P * &Ptr)     
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top