如何调用存储在向量中的对象的方法?下面的代码失败......如何通过矢量调用方法?
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