【问题标题】:Why C++ compiler (gcc) thinks function is `virtual' field?为什么 C++ 编译器 (gcc) 认为函数是“虚拟”字段?
【发布时间】:2010-10-27 17:41:11
【问题描述】:

我的班级中有以下方法定义:

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

出于某种原因,GCC 抱怨:

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

任何想法为什么它会认为 Compile 是一个字段,而不是一个方法?

【问题讨论】:

  • 你能告诉我们上面和下面的代码吗?是否还有其他错误。

标签: c++ gcc compiler-errors


【解决方案1】:

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

为了确定某个东西是对象还是函数声明,编译器有时必须扫描整个声明。声明中可能形成声明的任何构造都被视为声明。如果不是,那么任何这样的构造都被认为是一个表达式。 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)

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2020-06-18
    • 2015-11-12
    • 2013-08-08
    • 1970-01-01
    • 2014-06-24
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多