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

时间:2020-12-02 09:44:20

I have a the following method definition in my class:

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

virtual Calc* Compile(
  Evaluator* evaluator, ResolvedFunCall* fun_call, string* error);

For some reason, GCC complains that:

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

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

错误:'编译'声明为'虚拟'字段

Any ideas why it would believe Compile to be a field, instead of a method?

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

1 个解决方案

#1


I get that error when the first parameter doesn't make sense to it. Check that Evaluator is known as type:

当第一个参数对它没有意义时,我得到了那个错误。检查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

To find out whether something is a object or function declaration, the compiler sometimes has to scan the whole declaration. Any construct within the declaration that could possibly form a declaration is taken to be a declaration. If not, then any such construct is taken to be an expression. GCC apparently thinks because nonsense is no valid type, it can't be a valid parameter declaration, and thus falls back treating the whole declaration as a field (note that it says in addition error: expected ';' before '(' token ) . Same thing in local scope

要确定某些东西是对象还是函数声明,编译器有时必须扫描整个声明。声明中可能形成声明的任何构造都被视为声明。如果不是,那么任何这样的构造都被认为是表达式。 GCC显然认为因为废话不是有效类型,它不能是有效的参数声明,因此会将整个声明作为一个字段进行处理(注意它另外说明错误:期望';'之前'('标记)在本地范围内也是如此

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)

#1


I get that error when the first parameter doesn't make sense to it. Check that Evaluator is known as type:

当第一个参数对它没有意义时,我得到了那个错误。检查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

To find out whether something is a object or function declaration, the compiler sometimes has to scan the whole declaration. Any construct within the declaration that could possibly form a declaration is taken to be a declaration. If not, then any such construct is taken to be an expression. GCC apparently thinks because nonsense is no valid type, it can't be a valid parameter declaration, and thus falls back treating the whole declaration as a field (note that it says in addition error: expected ';' before '(' token ) . Same thing in local scope

要确定某些东西是对象还是函数声明,编译器有时必须扫描整个声明。声明中可能形成声明的任何构造都被视为声明。如果不是,那么任何这样的构造都被认为是表达式。 GCC显然认为因为废话不是有效类型,它不能是有效的参数声明,因此会将整个声明作为一个字段进行处理(注意它另外说明错误:期望';'之前'('标记)在本地范围内也是如此

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)