错误C2228:'.visitFile'的左边必须有class / struct / union类型

时间:2013-09-14 01:45:38

标签: c++

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);
}

问题是函数accept(Visitor* v){},我的编译器总是告诉我:

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

我该如何处理?

1 个答案:

答案 0 :(得分:2)

v是一个指针,应该调用operator->并且应该是*this

void File::accept(Visitor* v) {
    v->visitFile(*this);
//   ^^          ^
}