Sign in to follow this  
OnTheRun

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 this post


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

Add

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

to your vertex class.

Share this post


Link to post
Share on other sites
Thoooms    122
or even better, add:

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 this post


Link to post
Share on other sites
Mad__    122
Quote:
Original post by OnTheRun
Hi all,
I'm getting the error:

no match for 'operator[]' in ' this->CLoadObj::vertnormals[


Do you overloat operator[] for CLoadObj class ?

Share this post


Link to post
Share on other sites
snk_kid    1312
Quote:
Original post by Thoooms
or 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);
}


instead of repeating code.

Quote:
Original post by Thoooms
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]
}



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 this post


Link to post
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 this post


Link to post
Share on other sites
Thoooms    122
Quote:
Original post by snk_kid
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);
}



Ahh, that was actually rather nice.

Quote:
Original post by snk_kid
Quote:
Original post by Thoooms
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]
}



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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this