У меня очень глупая проблема с кодом, показанным ниже.
Цель состоит в том, чтобы увеличивать несколько счетчиков одновременно и печатать их значения после обработки предоставленным функтором.
Однако g ++ жалуется:
test.hpp:32: error: expected `;' before 'it' "
Я попытался добавить typedef, но сообщение об ошибке осталось. Вот код (упрощенная версия большего набора кода)
#include <vector>
#include <iostream>
template <class F>
class Counter
{
public:
Counter();
void increment(int amount);
private:
F calc;
int current_amount;
};
template <class F>
void Counter<F>::increment(int amount)
{
current_amount += amount;
std::cout << F(amount) << "\n";
}
template <class F>
class CounterBattery
{
public:
CounterBattery();
void incrementAll(int amount);
private:
std::vector<Counter<F> > counters;
};
template <class F>
void CounterBattery<F>::incrementAll(int amount)
{
for (std::vector<Counter<F> >::iterator it = counters.begin() ; it != counters.end() ; it++) // fails to compile here
it->increment(amount);
}
Я не понимаю, что я делаю не так с шаблонами здесь.
Спасибо за любую помощь, которую вы можете оказать