c++ - Using normal and overloaded operator at the same time -
i have class , overloaded [ operator use in main. problem here is; somewhere in class in function. want use [ operator old style. how can use them both or how can modify overloaded [ parameter ?
here overloading part.
t &operator[](int i) { if (i < 0 || >= arithmeticvector<t>::size()) throw std::string("out of bounds!"); //throw std::out_of_range; else return data[i]; };
and here function want use;
friend arithmeticvector<t> operator /(const arithmeticvector<t>& v1, const arithmeticvector<t>& v2 ) { /*do quick size check on vectors before proceeding*/ arithmeticvector<t> result(v1.size()); (unsigned int = 0; < result.size(); ++i) { result[i]=v1[i]/v2[i]; } return result; };
i error error c2678: binary '[' : no operator found takes left-hand operand of type 'const arithmeticvector'
the problem line.
result[i]=v1[i]/v2[i];
overloaded operator didn't :/
i can't understand, mean i want use [] operator old style
, should overload operator[]
const version too:
const t& operator[](int i) const { // ... }
v1
, v2
non-modifiable here, can't call non-const methods.
Comments
Post a Comment