我有一个程序,它存储在列表中的类和结构集。C++指针参考,类和指针的列表,奇怪的返回
它执行以下操作:
- 经过参考输入(一个
int
),迭代器,列表和指针到函数check()
- 迭代列表,直到它找到之间的匹配迭代器的数据和输入
- 将指针设置为迭代器的位置
- 根据是否找到匹配返回true或false。
我的问题是,当我调用该函数display()
从功能检查中,无论是从it->display()
,或Ptr->display()
,它工作正常。但是,当它通过参考传递出来,我尝试显示它。它打印垃圾。
//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
是比较私人数据在它的迭代类的功能,如
bool Property::checkExists(int input)
{
if (input == ID)
return true;
return false;
}
display
也很简单
void Property::display()
{
//Prints out property info
cout << ID << ";" << address << ";" << landTypes[type] << ";" << price << endl;
}
标准呼叫是(p
是我在程序中早些时候调用的Property
类的列表)
int input;
Property * temp; //Pointer to a class temp
list<Property>::iterator pIT;
cin >> input;
while(!check(input, pIT, p, temp)){
...
}
temp->display();
一个典型的输出将是(前两个函数内的调用和正确的,第三个是从外部调用temp->display();
功能。
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
编辑:对不起,我链接错误的显示功能()。编辑的代码更新
它在哪里通过引用传出? – 0x499602D2
bool Inspection :: check(int input,T it,T2 l,P *&Ptr) 在第一个代码块中,Where Ptr被推入函数中,然后再次返回 –
什么是您通过的'p'函数调用? – 0x499602D2