Home > Net >  The method visit() is undefined for the type T - Why?
The method visit() is undefined for the type T - Why?

Time:11-15

I can't understand why I'm getting The method visit() is undefined for the type T. Clearly, type T in here should be Node. Class Node has the visit() function. So what is the compiler complaining about? This code is supposed to be a Graph that accepts any data type as its nodes:

public class Graph<T>{

        public Map<T, List<T>> map = new HashMap<>();
    
        public void addEdge(T source, T destination, boolean bidirectional){
            if (!map.containsKey(source)){
                addVertex(source);
            }
            if (!map.containsKey(destination)){
                addVertex(destination);
            }
                if (!map.get(source).contains(destination)){
                    map.get(source).add(destination);
                }
                if (bidirectional){
                    if (!map.get(destination).contains(source)){
                        map.get(destination).add(source);
                    }
                }
        }
    
        public void bfs(T node){
            Queue<T> q = new LinkedList<>();
            node.visit();
            System.out.println(node.n);
            q.add(node);
            int len;
    
            while(!q.isEmpty()){
                T n = q.remove();
                n.visit();
                System.out.println(n.n);
                for (T i : map.get(n)){
                    if (!i.visited){
                        q.add(i);
                    }
                }
            }
        }
    
        static class Node{
            int n;
            boolean visited;
            public Node(int n){
                this.n = n;
                visited = false;
            }
            public void visit(){
                visited = true;
            }
            public void unvisit(){
                visited = false;
            }
        }
            
        public static void main(String[] args){
            Graph<Node> g = new Graph<>();
    
            Node zero = new Node(0);
            Node five = new Node(5);
            Node four = new Node(4);
            
            g.addEdge(zero, five, false);
            g.addEdge(zero, four, false);
    
            g.bfs(zero);            
        }
    }

CodePudding user response:

If T is only ever Node, remove the type from Graph and just use Node directly.

If T can be a variety of classes, the problem with just using T is T is unbounded - it's like T extends Object - but visit() is not defined for all objects.

The proper way to fix your problem is to define an interface:

interface Visitable {
    void visit();
}

Make Node implement it:

class Node implements Visitable {
    ...
}

Add a bound to T:

public class Graph<T extends Visitable> {
    ...
}

Repeat/combine for other methods as you like.


In this kind of situation, it's usual to type Node, which would have a field T value;. Perhaps that is what you intended?

  • Related