c++ - Cannot cast from vector<T> to T -
i have function takes arguments of type t such:
constexpr inline const bool isreflex(const t x1, const t y1, const t x2, const t y2, const t x, const t y)
calling function items form vector yiels error c2664: cannot convert argument 1 'vector<t, std::allocator<_ty>>' 'const t'
:
vector<t>* v = new vector<t>; // not creating vector myself, demonstration. // real vector passed const vector<t>* function executing following: if (isreflex(v[i-2], v[i-1], v[i], v[i+1], v[i+2], v[i+3])) // ^^^^^^ error
this makes little sense me, not passing vector rather contents. causing behaviour?
edit
ouch.
this because v
not vector, pointer vector. therefore, need dereference operator:
if (isreflex((*v)[i-2], (*v)[i-1], (*v)[i], (*v)[i+1], (*v)[i+2], (*v)[i+3]))
the reason error message may not entirely clear []
operator applies pointers well, , behaves dereference operator offset. in other words, c++ compiler treats variable v
built-in array of vectors, applies index [i-2]
array, , reports error, because type of v[i-2]
expression vector.
the real vector passed
const vector<t>*
function
you can make reference variable keep old syntax:
const vector<t> *pv // function parameter const vector<t>& v = *pv; // work if (isreflex(v[i-2], v[i-1], v[i], v[i+1], v[i+2], v[i+3])) { ... }
Comments
Post a Comment