VC9 Error 2668

I got Error 2668 on VC9 with following snippet,

template<class T>
class Visitor
{
public:
void visit(T& t)
{
std::cout << "visit " << typeid(t).name() << std::endl;
}
};

template<class T>
class Visitee
{
public:
void accept(Visitor<T>& v)
{
T& t = static_cast<T&>(*this);
v.visit(t);
}
};

class VisiteeA : public Visitee<VisiteeA>
{

};

class VisitorA : public Visitor<VisiteeA>
{

};

class VisiteeB : public Visitee<VisiteeB>, public VisiteeA
{
public:
using Visitee<VisiteeB>::accept;
using VisiteeA::accept;
};

class VisitorB : public Visitor<VisiteeB>, public VisitorA
{
public:
using Visitor<VisiteeB>::visit;
using VisitorA::visit;
};

int _tmain(int argc, _TCHAR* argv[])
{
VisiteeB* b = new VisiteeB;
VisitorB* v = new VisitorB;

b->accept(*v);

return 0;
}

And I got an error as following,1>------ Build started: Project: ccc, Configuration: Debug Win32 ------
1>Compiling...
1>ccc.cpp
1>e:\ccc\ccc.cpp(57) : error C2668: 'Visitee<T>::accept' : ambiguous call to overloaded function
1> with
1> [
1> T=VisiteeA
1> ]
1> e:\ccc\ccc.cpp(21): could be 'void Visitee<T>::accept(Visitor<T> &)'
1> with
1> [
1> T=VisiteeA
1> ]
1> e:\ccc\ccc.cpp(21): or 'void Visitee<T>::accept(Visitor<T> &)'
1> with
1> [
1> T=VisiteeB
1> ]
1> while trying to match the argument list '(VisitorB)'
1>Build log was saved at "file://e:\ccc\Debug\BuildLog.htm"
1>ccc - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

The code is trying to implement the visitor pattern in a better way: trying to match correct visitor before downcast. for example, I hope it calls VisitorB::visit() at last from the main() function.

The error shows it can't decide the version of member function accept() since VisiteeB got two parents:
class VisiteeB : public Visitee<VisiteeB>, public VisiteeA
{
public:
using Visitee<VisiteeB>::accept;
using VisiteeA::accept;
};

But I think Visitee<VisiteeB>::accept() is the obvious choice for it's the best match.
Last edited on
Topic archived. No new replies allowed.