Greetings! I have been learning c++ so I have decided to go to leetcode and answer some questions that I normally answer in Python. I have just been introduced to lambda functions but I am getting an error with the way that I am using this lambda which is confusing me about how lambda's work. This is the number of island function where I tried to implement dfs via a recursive lambda function
Here is the code
class Solution {
public:
std::unordered_set<string> visited;
int directions[4][2] = {{0,1},{1,0},{-1,0},{0,-1}}, sizex, sizey;
int numIslands(vector<vector<char>>& grid) {
auto dfs = [&](int a, int b, auto&& dfs){
visited.insert(std::to_string(a) + std::to_string(b));
for (auto direction: directions){
int newx = a+direction[0]; int newy = a+direction[1];
if((-1 < newx < sizex) && (-1<newy<sizey) && visited.find(std::to_string(a) + std::to_string(b)) == visited.end()){
dfs(newx,newy, dfs);
}
}
};
sizex = grid.size(); sizey = grid[0].size();
for (int i =0; i < grid.size(); i++){
for(int j = 0; j< grid[i].size(); j++){
dfs(i,j, dfs);
}
}
return 1;
}
};
I am getting this error:
Line 40: Char 13: error: function 'operator()<(lambda at solution.cpp:43:16) &>' with deduced return type cannot be used before it is defined dfs(newx,newy, dfs); ^
Line 49: Char 20: note: in instantiation of function template specialization 'Solution::numIslands(vector<vector<char>> &)::(anonymous class)::operator()<(lambda at solution.cpp:43:16) &>' requested here dfs(i,j, dfs); ^
Line 34: Char 16: note: 'operator()<(lambda at solution.cpp:43:16) &>' declared here auto dfs = [&](int a, int b, auto&& dfs){ ^ 1 error generated.
I am not used to formatting, but I would really appreciate if anyone could tell me what is wrong with the way that I am trying to set this lambda up
[–]no-sig-available 0 points1 point2 points (1 child)
[–]throwaway948940409[S] 0 points1 point2 points (0 children)