Search code examples
c++stl

how to resolve Error C2440 'initializing': cannot convert from '_Ty' to '_Objty'


I am trying to write an algorithm for finding a subset of a given vector that has a certain requested sum. I am trying to use the 'meet in the middle' technique as described in 'the competitive programmer's handbook' by Antti Laaksonen. here is my code:

vector<int> subset_with_sum1(vector<int> set, int requested_sum) {
    int half = set.size() / 2;
    vector<int> set_a(set.begin(), set.begin() + half),
        set_b(set.begin() + half, set.end());
    auto subsets_a = get_subsets1(set_a),
        subsets_b = get_subsets1(set_b);
    vector<int> sums_a(subsets_a.size()), sums_b(subsets_b.size());
    transform(
        subsets_a.begin(),
        subsets_a.end(),
        sums_a.begin(),
        sum_vector
    );
    transform(
        subsets_b.begin(),
        subsets_b.end(),
        sums_b.begin(),
        sum_vector
    );
    for (int i = 0; i < sums_a.size(); i++)
        if (sums_a[i] == requested_sum)
            return subsets_a[i];
    for (int i = 0; i < sums_b.size(); i++)
        if (sums_b[i] == requested_sum)
            return subsets_b[i];
    for (int i = 0; i < sums_a.size(); i++)
        for (int j = 0; j < sums_b.size(); j++)
            if ((sums_a[i] + sums_b[j]) == requested_sum) {
                vector<int> temp(subsets_a.begin(), subsets_a.end());
                temp.insert(temp.end(),subsets_b.begin(), subsets_b.end());
                return temp;
            }
}

this code produces the error mentioned in the title. here is the full error:

Error   C2440   'initializing': cannot convert from '_Ty' to '_Objty'   PracticeIOI C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.24.28314\include\xmemory 671

this error appears in other questions on this website but always in relation to some user-defined classes and pointers, neither of which I use. the same goes for the Microsoft docs. also, as the error occurs in an STL file, I can't even know which specific line of my code causes it. how can I fix this?

EDIT: I didn't include some code that is necessary for running the function so here it is:

vector<vector<int>> get_subsets1(vector<int> nums) {
    int full = 1 << nums.size();
    bitset<8 * sizeof(int)> set;
    vector<vector<int>> subsets;
    vector<int> temp;
    for (int i = 0; i < full; i++) {
        set = bitset<8 * sizeof(int)>(i);
        temp = vector<int>();
        for (int j = 0; j < nums.size(); j++)
            if (set[j])
                temp.push_back(nums[j]);
        subsets.push_back(temp);
    }
    return subsets;
}
int sum_vector(vector<int> vec) {
    return accumulate(vec.begin(), vec.end(), 0);
}

Solution

  • This issue is on these lines:

    vector<int> temp(subsets_a.begin(), subsets_a.end());
    temp.insert(temp.end(),subsets_b.begin(), subsets_b.end());
    

    Here temp is of type vector<int>, but subsets_a and subsets_b are of type vector<vector<int>>. Since the value types are int and vector<int> respectively, they don't match, and so this doesn't compile.

    Depending on what you need, either make temp a vector<vector<int>>, or copy the individual ints in each vector of subsets_a and subsets_b into temp.