# no match for 'operator[]'

## Recommended Posts

OnTheRun    122
Hi all, I'm getting the error: no match for 'operator[]' in ' this->CLoadObj::vertnormals[ In this line: vertnormals[vertices[objFace[i].Face[0]]]=vertnormals[vertices[objFace[i].Face[0]]]+n; vertnormals: CVert vertnormals[50000]; CVert: struct CVert { union { float x, y, z; float v[3]; }; }; What am I doing wrong? Thanks & Greetings

##### Share on other sites
Ilici    862
I can't tell precisely, but anyhow there is no operator "+" defined for CVert.

CVert operator+(CVert v)
{
return CVert(x + v.x, y + v.y, z + v.z);
}

##### Share on other sites
Thoooms    122
CVert operator+(const CVert& v) const{return CVert(x + v.x, y + v.y, z + v.z);}

This avoids the copying of the entire right operand when performing the + operation and is also const-correct.

This does not fix your original problem though, you are trying to index the vector directly with the [] operator, you could overload the operator like this:

float& operator[](const unsigned int i) const{return v[i]}

this might - or might not - be how you want your semantics of the [] operator. A better solution, perhaps, is to just use

CVert vert;float foo = vert.v[i]

instead of whay you currently do:
CVert vert;float foo = vert[i]

##### Share on other sites
Quote:
 Original post by OnTheRunHi all,I'm getting the error:no match for 'operator[]' in ' this->CLoadObj::vertnormals[

Do you overloat operator[] for CLoadObj class ?

##### Share on other sites
snk_kid    1312
Quote:
 Original post by Thooomsor even better, add:CVert operator+(const CVert& v) const{return CVert(x + v.x, y + v.y, z + v.z);}

or the best way is build the general arithmetic operators in terms of arithmetic assignement operators and make the general ones as free functions e.g.

template < typename T >struct vector3 {   T x, y, z;   /* .... */   vector3<T>& operator+=(const vector3<T>& v) { //implicitly inline      x += v.x, y += v.y, z += v.z;      return *this;   }};template < typename T >inline vector3<T> operator+(const vector3<T>& v, const vector3<T>& u) {   return (vector3<T>(v) += u);}

Quote:
 Original post by Thooomsyou are trying to index the vector directly with the [] operator, you could overload the operator like this:float& operator[](const unsigned int i) const{return v[i]}

there is subtle logic errors, its a constant method that returns a non-constant reference to the element so its a bit of contradiction, you wont 2 versions of overloaded sub-script operators one non-constant method that returns a non-constant reference and second version that is constant e.g.

#include <cstddef>template < typename T >struct vector3 {   typedef size_t size_type;   /* ... */   const T& operator[](size_type i) const { return v[i]; }   T& operator[](size_type i) { return v[i]; }};

yes you can overload just on the constant-ness of a member function.

##### Share on other sites
Oluseyi    2116
What, no bounds checking?

The real question, and I'm surprised nobody asked it, is what the context is. Does the statement occur within a method of CLoadObj? Why is the error reported as being in this->CLoadObj::vertnormals? That's an unusual syntax.

Without more information, we can't help you for sure. All we can do is take shots in the dark and hope we hit something.

##### Share on other sites
Thoooms    122
Quote:
 Original post by snk_kidor the best way is build the general arithmetic operators in terms of arithmetic assignement operators and make the general ones as free functions e.g.template < typename T >struct vector3 { T x, y, z; /* .... */ vector3& operator+=(const vector3& v) { //implicitly inline x += v.x, y += v.y, z += v.z; return *this; }};template < typename T >inline vector3 operator+(const vector3& v, const vector3& u) { return (vector3(v) += u);}

Ahh, that was actually rather nice.

Quote:
Original post by snk_kid
Quote:
 Original post by Thooomsyou are trying to index the vector directly with the [] operator, you could overload the operator like this:float& operator[](const unsigned int i) const{return v[i]}

there is subtle logic errors, its a constant method that returns a non-constant reference to the element so its a bit of contradiction, you wont 2 versions of overloaded sub-script operators one non-constant method that returns a non-constant reference and second version that is constant e.g.
oad just on the constant-ness of a member function.

Well, I won't call that error "subtle", it's simply a typo on my part and that code won't work.