I want to use c++ 11 "for loop" to iterate elements of a vector but i receive some errors ( begin() and end() functions i think is a problem).Thanks!
#include <iostream>
#include <vector>
#include<iterator>
using namespace std;
template<typename TElement>
class MyClass {
private:
vector<TElement> vec;
public:
MyClass& operator+(TElement n) {
vec.push_back(n);
return *this;
}
int getS() {
return vec.size();
}
iterator begin() {//here is some problems
return vec.begin();
}
iterator end() {
return vec.end();
}
};
int main() {
MyClass<int> mm;
mm = mm + 10;
mm = mm + 9;
double avg = 0.0;
for (auto g : mm) { //begin() and end() functions error
avg += mm;
}
cout<< avg / mm.getS();
return 0;
}
errors:
C2675 unary '++': 'std::iterator' does not define this operator or a conversion to a type acceptable to the predefined operator,
illegal indirection,
C2678 binary '!=': no operator found which takes a left-hand operand of type 'std::iterator' ,
C2955 'std::iterator': use of class template requires template argument list,
C2514 'std::iterator': class has no constructors