如何調用存儲在向量中的對象的方法?下面的代碼失敗......如何通過矢量調用方法?
ClassA* class_derived_a = new ClassDerivedA;
ClassA* class_another_a = new ClassAnotherDerivedA;
vector<ClassA*> test_vector;
test_vector.push_back(class_derived_a);
test_vector.push_back(class_another_a);
for (vector<ClassA*>::iterator it = test_vector.begin(); it != test_vector.end(); it++)
it->printOutput();
的代碼檢索以下錯誤:
test3.cpp:47: error: request for member ‘printOutput’ in ‘* it.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator-> with _Iterator = ClassA**, _Container = std::vector >’, which is of non-class type ‘ClassA*’
的問題似乎是it->printOutput();
但此刻,我不知道如何正確地調用該方法,有人知道嗎?
關於mikey
作品,謝謝! – 2010-05-14 10:43:38
循環的數字本來就是這裏比較容易的解決方案。在絕大多數情況下迭代器不需要存在實際的迭代 - 僅用於算法。 – Puppy 2010-05-14 12:51:21
@DeadMG有趣 - 你如何迭代列表或地圖(例如),而不使用迭代器? – 2010-05-14 12:53:46