I got a weird compile error in the following code:
class Solution {
public:
int function(vector<int>& nums) {
//Did something
int len = nums.size();
int mid = len / 2;
if (nums[mid] < nums[0]) {
vector<int> newVec(nums.begin(), nums.begin() + mid);
return function(newVec);
}
else
return function(vector<int>(nums.begin() + mid + 1, nums.end()));
}
};
The error occured in:
return function(vector<int>(nums.begin() + mid + 1, nums.end()));
It seems to me that it works perfectly fine if I construct the vector first, then pass it to the function. Like what I did in the block under if
:
vector<int> newVec(nums.begin(), nums.begin() + mid);
return function(newVec);
But if I combine these two steps, the error occurs. Could anyone please explain that for me? Many thanks~
=========================================================================
Edit:
The compile error was
no matching function for call to ‘Solution::function(std::vector<int>)’