[英]C++, Pushing into List<Class*>, vector<Class> iterator
我得到了vector <C_Student>,並且我必須創建一個返回List <C_Student *>的函數。 現在我在函數中有這個
list<C_Student*> listt;
for (vector<C_Student>::iterator it = V_Students.begin(); it != V_Students.end(); it++)
if (it->GetDegreeByDisciplineName(disciplineName) == degree)
listt.push_back(*it);
這使
Description Project File Line Suppression State Error (active) E0304 no instance of overloaded function "std::list<_Ty, _Alloc>::push_back [with _Ty=C_Student *, _Alloc=std::allocator<C_Student *>]" matches the argument list C++_Project
我做錯了什么?
通過
listt.push_back(*it);
您嘗試將C_Student
的副本存儲在列表中。 要將指針推入列表,您需要使用&
運算符取消引用*it
對象:
listt.push_back(&*it);
修改vector時,vector中的元素將失效,這可能導致列表中的指針懸空。 希望您知道。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.