2

Visitor.h

class Visitor{
public:
    virtual ~Visitor() {}
    virtual void visitNode(Node*) = 0;
    virtual void visitFile(File*) = 0;
    virtual void visitDirectory(Directory*) = 0;
    virtual void visitLink(Link*) = 0;
protected:
    Visitor();
    Visitor(const Visitor&);
};
//realize those visit function
void Visitor::visitNode(Node* n) {
    //common default behavior
    cerr << "It is not a directory! " << endl;
}
void Visitor::visitDirectory(Directory* d) {
    Visitor::visitNode(d);
}
void Visitor::visitFile(File* f) {
    Visitor::visitNode(f);
}
void Visitor::visitLink(Link* l) {
    Visitor::visitNode(l);
}

File.h

class File : public Node {
public:
    File();
    //redeclare common interface here
    void setName(string& name);
    string& getName();
    void setCDate(char* cDate);
    char* getCDate();
    long size();
    virtual void accept(Visitor*) = 0;
private:
    string& name;
    char* cDate;
};
//realize the function accept
void File::accept(Visitor* v) {
    v.visitFile(this);
}

The problem is the function accept(Visitor* v){}, my compiler always tell me:

d:\win7 data\data\c\filemanage\file.h(20) : error C2228: left of '.visitFile' must have class/struct/union type

How can I deal with it?

4

1 回答 1

2

v is a pointer, should call operator-> and should be *this

void File::accept(Visitor* v) {
    v->visitFile(*this);
//   ^^          ^
}
于 2013-09-14T01:46:49.293 回答