What is this compile error mean?


  • 0
    L

    required from 'class std::unordered_setSolution::Pos'


  • 0
    L

    HERE IS MYCODE

    struct Pos
    {
    	Pos(int i, int j):x(i), y(j){}
    	int x;
    	int y;
    };
    
    class Solution {
    public:
    
        void solve(vector<vector<char> > &board) {
    		int n= board.size();
    		if(n < 3) return;
            bool** bVisited = new bool*[n];
    		for(int i=0; i<n; i++)
    			bVisited [i] = new bool[n];
    		for(int i=0; i<n; i++)
    			for(int j=0; j<n; j++)
    				bVisited[i][j] = false;
    		unordered_set<Pos> pos;
    		for(int i=0; i<n; i++)
    		{
    			if(board[0][i] == 'O' && !bVisited[0][i])
    				dfs(board, pos, bVisited, 0, i);
    			if(board[n-1][i] == 'O' && !bVisited[n-1][i])
    				dfs(board, pos, bVisited, n-1, i);
    
    			if(board[i][0] == 'O' && !bVisited[i][0])
    				dfs(board, pos, bVisited, i, 0);
    			if(board[i][n-1] == 'O' && !bVisited[i][n-1])
    				dfs(board, pos, bVisited, i, n-1);
    		}
    
    		for(int i=0; i<n; i++)
    			for(int j=0; j<n; j++)
    			{
    				if(pos.find(Pos(i, j)) == pos.end())
    					board[i][j] = 'X';
    			}
    
    		for(int i=0; i<n; i++)
    			delete [] bVisited[i];
    		delete [] bVisited;
        }
    private:
    	void dfs(vector<vector<char> >& board, unordered_set<Pos>& pos, bool** visited, int x, int y)
    	{
    		int n = board.size();
    		if(x < 0 || y < 0 || x>=n || y>=n) return;
    		if(visited[x][y]) return;
    		if(board[x][y] == 'X') return;
    		pos.insert(Pos(x, y));
    		visited[x][y] = true;
    		dfs(board, pos, visited, x-1, y);
    		dfs(board, pos, visited, x+1, y);
    		dfs(board, pos, visited, x, y-1);
    		dfs(board, pos, visited, x, y+1);
    	}
    };

  • 1
    S

    That is because you attempt to use a user-defined type 'Pos' for unordered_set, which by itself does not know how to hash this data type. You must implement your own hashing policy for unordered_set with such a user type. A simple way to circumvent this problem is to represent the 2-d coordinate to a 1-d index.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.