| | |
Problem implementing Visitor Pattern with Abstract visitor class
Please support our Computer Science advertiser: Learn about neural networks and artificial intelligence.
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 5
Reputation:
Solved Threads: 0
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):
And I have a concreteVisitorA class, which inherits from an abstract visitor class
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
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?
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
- Using abstract base class, invalid conversion (C++)
- Implementing a visitor counter (ASP.NET)
- Code Snippet: Delegate-based Visitors (C#)
- Question regarding base class and derived class (C++)
- stuck with abstract class, function overriding (C++)
- UML class digram help (Java)
- problem with an abstract class (C#)
- What is the difference between abstract class and interface (Java)
- Pythonwin/import/class-related problem (Python)
- Abstract class homework problem (C++)
Other Threads in the Computer Science Forum
- Previous Thread: LCR-Leader Election
- Next Thread: Final year Project for Computer Science
| Thread Tools | Search this Thread |
ai algorithm algorithms amazon assignment assignments automata battery bigbrother binary bizarre bletchleypark blogging bomb business cern codebreaker compiler computer computers computerscience computertrackingsoftware connect conversion csc data dataanalysis dataintepretation development dfa dissertation dissertationthesis dissertationtopic ebook energy extensions floatingpoint foreclosure foreclosuresoftware fuel gadgets geeks givemetehcodez government graphics hardware history homeowners homeworkassignment homeworkhelp humor ibm idea ideas internet iphone ipod itcontracts jobs kindle laser laws linkbait lsmeans mainframes mining mobileapplication msaccess nano netbeans news os p2p piracy piratebay principles programming rasterizer research sam-being-cute sas science security sex simulation software spying sql stephenfry study supercomputer sweden technology textfield turing turingtest two'scompliment virus warehouse ww2





