为什么C++编译器(gcc)认为函数是"虚拟"字段?

Laj*_*agy 11 c++ gcc compiler-errors

我的课程中有以下方法定义:

virtual Calc* Compile(
  Evaluator* evaluator, ResolvedFunCall* fun_call, string* error);
Run Code Online (Sandbox Code Playgroud)

出于某种原因,海湾合作委员会抱怨说:

error: 'Compile' declared as a 'virtual' field

任何想法为什么它会相信Compile是一个领域,而不是一个方法?

Joh*_*itb 31

当第一个参数对它没有意义时,我得到了那个错误.检查Evaluator是否称为类型:

struct A {
    virtual void* b(nonsense*, string*);
};

=> error: 'b' declared as a 'virtual' field

struct A {
    virtual void* b(string*, nonsense*);
};

=> error: 'nonsense' has not been declared
Run Code Online (Sandbox Code Playgroud)

为了确定某些东西是对象还是函数声明,编译器有时必须扫描整个声明.声明中可能形成声明的任何构造都被视为声明.如果不是,那么任何这样的构造都被认为是表达式.GCC显然认为因为nonsense没有有效的类型,所以它不能成为有效的参数声明,因此可以将整个声明作为一个字段进行处理(注意它另外说明error: expected ';' before '(' token).在本地范围内也是如此

int main() {
    int a;

    // "nonsense * a" not treated as declaration
    void f(nonsense*a);
}

=> error: variable or field 'f' declared void

int main() {
    // "nonsense * a" treated as parameter declaration
    typedef int nonsense;
    void f(nonsense*a);
}

=> (compiles successfully)
Run Code Online (Sandbox Code Playgroud)