K
klaas
the following code gives rise to the beneath error message, only when
a matrix object is instantiated as matrix<bool>, not with matrix<float>:
/*returns a reference to the object at position (Row,Col) in matrix*/
template <class num_type,template <class T> class functor>
num_type & matrix<num_type,functor >:perator()(const int Row,const
int Col)
{if (Row<rows && Col<cols)
{vector<num_type> & x=matrix_core[Row];
//num_type & y=x[Col];<-is also faulty ,
//return matrix_core[Row][Col]; <- same problem
return x[Col];
}
// else return (*num_type)0;
}
indeed, matrix_core is of type vector<vector<num_type> >
apemonkie.cpp:27: instantiated from here
matrix.cpp:301: could not convert `std::vector<bool,
_Alloc>:perator[](unsigned int) [with _Alloc =
std::allocator<bool>](Col)'
to `bool&'
make: *** [apemonkie.o] Error 1
I really do need a reference since I need to change values inside the
matrix.
STL-manual says that for a vector<T> foo;
the expression foo[bar]; should return a reference to an object of type T.
especially when you read the following, taken out of the bit_vector manual:
So it seems that there is special behavior for vector<bool> which sounds
reasonable (bit_vector is the same thing right), but how can I get the
references?
thanks in advance,
klaas
a matrix object is instantiated as matrix<bool>, not with matrix<float>:
/*returns a reference to the object at position (Row,Col) in matrix*/
template <class num_type,template <class T> class functor>
num_type & matrix<num_type,functor >:perator()(const int Row,const
int Col)
{if (Row<rows && Col<cols)
{vector<num_type> & x=matrix_core[Row];
//num_type & y=x[Col];<-is also faulty ,
//return matrix_core[Row][Col]; <- same problem
return x[Col];
}
// else return (*num_type)0;
}
indeed, matrix_core is of type vector<vector<num_type> >
apemonkie.cpp:27: instantiated from here
matrix.cpp:301: could not convert `std::vector<bool,
_Alloc>:perator[](unsigned int) [with _Alloc =
std::allocator<bool>](Col)'
to `bool&'
make: *** [apemonkie.o] Error 1
I really do need a reference since I need to change values inside the
matrix.
STL-manual says that for a vector<T> foo;
the expression foo[bar]; should return a reference to an object of type T.
especially when you read the following, taken out of the bit_vector manual:
reference in "reference operator[](size_type n)"
A proxy class that acts as a reference to a single bit;
the reason it exists is to allow expressions like V[0] = true.
(A proxy class like this is necessary, because the C++ memory
model does not include independent addressing of objects smaller
than one byte.) The public member functions of reference are operator
bool() const, reference& operator=(bool), and void flip(). That is,
reference acts like an ordinary reference: you can convert a reference
to bool, assign a bool value through a reference, or flip the bit that
a reference refers to.
So it seems that there is special behavior for vector<bool> which sounds
reasonable (bit_vector is the same thing right), but how can I get the
references?
thanks in advance,
klaas