iteong iteong - 3 months ago 14
C++ Question

error: request for member ‘end’ in .... which is of non-class type ‘double’

I got the following error which I suspect has something to do with the mismatch of types or something.... The magnitude_ variable that is used in the function is a double array declared as a pointer (e.g. double *magnitude_;) and in the function I'm trying to get the begin and end of the double array, but it doesn't seem to work.. I also tried

for (auto iter = std::begin(magnitude_); iter != std::end(magnitude_); ++iter)


but it doesn't seem to work. Can anyone advise me on this?:

EuclideanVector.cpp: In member function ‘void evec::EuclideanVector::updateNormal()’:
EuclideanVector.cpp:180:32: error: request for member ‘begin’ in ‘*((evec::EuclideanVector*)this)->evec::EuclideanVector::magnitude_’, which is of non-class type ‘double’
for (auto iter = magnitude_->begin(); iter != magnitude_->end(); ++iter)
^~~~~
EuclideanVector.cpp:180:61: error: request for member ‘end’ in ‘*((evec::EuclideanVector*)this)->evec::EuclideanVector::magnitude_’, which is of non-class type ‘double’
for (auto iter = magnitude_->begin(); iter != magnitude_->end(); ++iter)


The function in the .cpp file is:

void EuclideanVector::updateNormal()
{
double normal_tmp = 0.0;

for (auto iter = magnitude_->begin(); iter != magnitude_->end(); ++iter)
{
normal_tmp += pow(*iter, 2);
}

*normal_ = sqrt(normal_tmp);

}

Answer

You need to also know the size of the array (e.g. num_elements), then you can use pointer arithmetic

for (double* iter = magnitude_; iter != (magnitude_ + num_elements); ++iter)
{
    double value = *iter;
}

Or simply use indexing

for (int i = 0; i < num_elements; ++i)
{
    double value = magnitude_[i];
}

Then only way std::begin and std::end would have worked is if the array size was known in that scope, e.g.

double magnitude_[10];
for (auto iter = std::begin(magnitude_); iter != std::end(magnitude_); ++iter)
{
}

I'd suggest either switching to std::array or std::vector depending on how your array is allocated, then you can use these methods more easily.