944,179 Members | Top Members by Rank

Ad:
Oct 9th, 2009
0

Problem implementing Visitor Pattern with Abstract visitor class

Expand Post »
I posted this in the C++ forum, but decided to repost here as it relates to software development, and this might be a better audience for it.

I'm trying to implement a Visitor Pattern in c++, but I'm having problems with the elements being visited in turn having their children visited. For example I have a class Element, which has a vector of Nodes (can be elements or any classes that inherit from node):
class Element : public Node{
	public:
		Element();
		Element(string n, string v = " ");
		~Element();
		void Accept(Visitor& v);
		vector<Node*> nodeVec;
		vector<Attr*> attrVec;
	private:
		string name, value;
};

void Element::Accept(Visitor& v)
{
	v.VisitElement(*this);
}

And I have a concreteVisitorA class, which inherits from an abstract visitor class
 
class ConcreteVisitorA : public Visitor {
	public:
		ConcreteVisitorA();
		~ConcreteVisitorA();
		void VisitElement(Element e);
};

void ConcreteVisitorA::VisitElement(Element e)
{
	int numNodes = e.GetNumNodes();
	int numAttrs = e.GetNumAttrs();
	cout << "Visitor has found " << numNodes << " noes\n";
	e.nodeVec[0]->Accept(this);
		
	}

The problem is when it gets to the last line of VisitElement, where the first member of the vector is visited i get the error

concreteVisitorA.cc:20: error: no matching function for call to 'Node::Accept(ConcreteVisitorA* const)'
node.h:9: note: candidates are: virtual void Node::Accept(Visitor&)

The Accept method has to have visitor passed to it by reference, because it is abstract. But "this" is a pointer and the whole thing craps out. Any suggestions?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hoolr is offline Offline
5 posts
since Oct 2009
Oct 9th, 2009
0

Solved

I simply added another implementation of accept, which could take take a pointer and now it works.

void Element::Accept(Visitor& v)
{
	v.VisitElement(*this);
}
void Element::Accept(Visitor* v)
{
	v->VisitElement(*this);
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hoolr is offline Offline
5 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Computer Science Forum Timeline: LCR-Leader Election
Next Thread in Computer Science Forum Timeline: Weird website behavior





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC