Visitor Pattern "does not implement interface" error

justHelloWorld

It's first time that I try to implement the visitor pattern (using c#), I'm sorry if I did something wrong! However, I implemented this visitor pattern:

public interface VisitableElement
{
    void accept (VisitorElement visitor); 
}

public interface VisitorElement
{
    void visit (VisitableElement visitable);
}

public class SomeClass : VisitableElement
{
    ...
    public void accept(VisitorElement visitor)
    {
        ...
    }
}
//here there are other classes which implement VisitableElement (not useful for the example)

class Visitor: VisitorElement
{
...
    public void visit (SomeClass someobject)
    {
        ...
    }
//othes implementation of visit (one for each element of class of the hierarcy)
...
}

But when I try to compile this error is returned:

Error   1   'Visitor' does not implement interface member 'NameSpace.VisitorElement.visit(NameSpace.VisitableElement)'

...But why? I implemented it through visit(SomeClass someobject)!

The funny thing is that if I try to add:

    public void visit(VisitableElement visitable)
    {
        this.visit(visitable);
    }

Then everything works fine, but it should be unecessary to implement it in this way (and the first visit method should be enough!)

sloth

The interface VisitorElement requires that your class implements the method void visit (VisitableElement visitable).

The visit method you implemented does not fulfill this requirement, since it only allows arguments of type SomeClass.

While each SomeClass is a VisitableElement, the interface requires that the method accepts any type that implements VisitableElement, not only SomeClass.


If you want your visit method to only accept arguments of type SomeClass, consider using a generic VisitorElement<T> and an explicit interface implementation:

public interface VisitableElement
{
    void accept (VisitorElement visitor); 
}

public class SomeClass : VisitableElement
{
    public void accept(VisitorElement visitor) { }
}

public interface VisitorElement
{
    void visit (VisitableElement visitable);
}

public interface VisitorElement<T> : VisitorElement where T : VisitableElement
{
    void visit (T visitable);
}

class Visitor: VisitorElement<SomeClass>
{
    public void visit (SomeClass someobject) { }

    void VisitorElement.visit(VisitableElement visitable) { }
}

This way, you can only call the visit (SomeClass someobject) overload as long as you don't cast your Visitor instance to the non-generic VisitorElement. In the non-generic visit method, you could try to cast visitable to SomeClass and call the generic version as fallback (or do whatever you like).


As it is not entirely clear to me what you want to achieve, it's hard to say what is the best thing to do in your case.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Error: Does not implement interface member

From Dev

Error: Does not implement interface member

From Dev

Error 1: does not implement interface member

From Dev

Window.getComputedStyle does not implement interface Element error in Firefox

From Dev

Error 'open' called on an object that does not implement interface XMLHttpRequest

From Dev

Xamarin jar binding error: Class does not implement interface member

From Dev

Meteor angularjs "Node.replaceChild does not implement interface Node" error

From Dev

"Class does not implement interface member" error on class implementing a generic interface

From Dev

The "does not implement interface member" error when using inheritance

From Dev

Error when upgrading to WebAPI 2.1 XmlDocumentationProvider does not implement interface member GetDocumentation

From Dev

Angular error: Node.replaceChild does not implement interface Node.replaceWith

From Dev

Does not implement interface member issues

From Dev

Class does not implement interface member

From Dev

IEnumerator does not implement interface IEnumerable

From Dev

Does not implement interface member MVC

From Dev

Visitor pattern with multiple argument

From Dev

Visitor pattern in python

From Dev

Simplified visitor pattern

From Dev

visitor pattern in PHP

From Dev

Pattern "Visitor" or a dynamic cast?

From Dev

OCaml visitor pattern

From Dev

sequencing events with visitor pattern

From Dev

Visitor Design Pattern - eCommerce

From Dev

specialise for loop with visitor pattern?

From Dev

Pattern "Visitor" or a dynamic cast?

From Dev

Visitor pattern with multiple argument

From Dev

Simplified visitor pattern

From Dev

specialise for loop with visitor pattern?

From Dev

Visitor pattern and logical operations

Related Related

  1. 1

    Error: Does not implement interface member

  2. 2

    Error: Does not implement interface member

  3. 3

    Error 1: does not implement interface member

  4. 4

    Window.getComputedStyle does not implement interface Element error in Firefox

  5. 5

    Error 'open' called on an object that does not implement interface XMLHttpRequest

  6. 6

    Xamarin jar binding error: Class does not implement interface member

  7. 7

    Meteor angularjs "Node.replaceChild does not implement interface Node" error

  8. 8

    "Class does not implement interface member" error on class implementing a generic interface

  9. 9

    The "does not implement interface member" error when using inheritance

  10. 10

    Error when upgrading to WebAPI 2.1 XmlDocumentationProvider does not implement interface member GetDocumentation

  11. 11

    Angular error: Node.replaceChild does not implement interface Node.replaceWith

  12. 12

    Does not implement interface member issues

  13. 13

    Class does not implement interface member

  14. 14

    IEnumerator does not implement interface IEnumerable

  15. 15

    Does not implement interface member MVC

  16. 16

    Visitor pattern with multiple argument

  17. 17

    Visitor pattern in python

  18. 18

    Simplified visitor pattern

  19. 19

    visitor pattern in PHP

  20. 20

    Pattern "Visitor" or a dynamic cast?

  21. 21

    OCaml visitor pattern

  22. 22

    sequencing events with visitor pattern

  23. 23

    Visitor Design Pattern - eCommerce

  24. 24

    specialise for loop with visitor pattern?

  25. 25

    Pattern "Visitor" or a dynamic cast?

  26. 26

    Visitor pattern with multiple argument

  27. 27

    Simplified visitor pattern

  28. 28

    specialise for loop with visitor pattern?

  29. 29

    Visitor pattern and logical operations

HotTag

Archive