1
votes

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?

2
The only thing valid to pass into the void* here is a std::vector<std::vector<int> >*. Don't use void* when you can use templates!Pubby
Its a pointer to a vector of vectors. You need to dereference the pointer before you start drilling into the operator[]. I.e. (*datavector)[i][0]WhozCraig
Also note that sizeof(a) is equivalent to sizeof(int*). I suspect this isn't what you intended to do here.Ben Cottrell
Can this be considered a typo question?Shoe
@Pubby - I don't have a choice in the matter. The functions are defined as having void* and I can't change them.iamthesgt

2 Answers

4
votes

Your datavector variable is a pointer to a vector of vectors of int, so you should apply proper dereferencing:

sumweight += a[i] * (*datavector)[i][0];
//                  ^^^^^^^^^^^^^

sumvalue += a[i] * (*datavector)[i][1];
//                 ^^^^^^^^^^^^^

Moreover, this:

for (unsigned i=0; i < sizeof(a); i++)
//                     ^^^^^^^^^

Will not count the number of elements in the array (which makes me think that the k argument, which you are not using, is meant to contain the length of that array).

1
votes

First, your loop is wrong:

for(unsigned i=0; i<sizeof(a); i++)

sizeof(a) will always be sizeof(int*) because you cannot pass arrays to functions, regardless of your signature. The argument will be a pointer, so you'll never be accessing your vectors properly.

Your next problem is that you have a pointer to a vector (bad idea in general), but you are not dereferencing it properly You need one dereference before indexing into the next vector (and then into the next vector...), i.e.,

sumweight += a[i] * (*datavector)[i][0];

Next, ask yourself; why are you dealing with pointers to vectors of pointers to vectors? It's a very sloppy and error prone solution.