gpt4 book ai didi

c++ - 为什么 C++ 编译器 (gcc) 认为函数是 `virtual' 字段?

转载 作者:可可西里 更新时间:2023-11-01 14:54:04 26 4
gpt4 key购买 nike

我的类中有以下方法定义:

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

出于某种原因,GCC 提示说:

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

为什么它会认为 Compile 是一个字段而不是方法?有什么想法吗?

最佳答案

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

关于c++ - 为什么 C++ 编译器 (gcc) 认为函数是 `virtual' 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/889581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com