I am writing a backtracking problem for my class and I have to implement some existing functions. This is one of the functions I have to implement. The void *input
is where we are supposed to pass in the data, which in my case is a two-dimensional vector.
void process_solution(int a[], int k, void *input, bool *finished)
{
int sumweight = 0;
int sumvalue = 0;
std::vector<std::vector<int> > *datavector = static_cast<std::vector<std::vector<int> >* >(input);
for(unsigned i=0; i<sizeof(a); i++)
{
sumweight += a[i]*datavector[i][0];
sumvalue += a[i]*datavector[i][1];
}
}
However, I believe something is wrong with my cast, because I get an error at the line sumweight += a[i]*datavector[i][0];
error: no match for 'operator*' in '*(a + ((long long unsigned int)(((long long unsigned int)i) * 4ull))) * (datavector + ((long long unsigned int)(((long long unsigned int)i) * 24ull)))->std::vector<_Tp, _Alloc>::operator[] with _Tp = std::vector, _Alloc = std::allocator >, std::vector<_Tp, _Alloc>::reference = std::vector&, std::vector<_Tp, _Alloc>::size_type = long long unsigned int'
If I try to use another variable to access the vector, like int t1 = datavector[i][0];
, I get an error
error: cannot convert 'std::vector' to 'int' in initialization
I thought a void pointer could be used to pass anything, so long as the proper cast was made. What is going on here?
void*
here is astd::vector<std::vector<int> >*
. Don't usevoid*
when you can use templates! – Pubbyoperator[]
. I.e.(*datavector)[i][0]
– WhozCraigsizeof(a)
is equivalent tosizeof(int*)
. I suspect this isn't what you intended to do here. – Ben Cottrell