C++ “预期的 '(' 函数式强制转换或类型构造”错误是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40063467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does the "Expected '(' for function-style cast or type construction" error mean?
提问by Cecilia
I'm getting the error "Expected '(' for function-style cast or type construction", and have done my best to research the meaning of this error online, but have been unable to find any documentation of what causes this error.
我收到错误“预期的 '(' for function-style cast or type construction”,并已尽最大努力在线研究此错误的含义,但无法找到导致此错误的原因的任何文档。
All the related questions on Stack Overflow that I have found bug fix a specific code snippet and do not explain more generally what is causing the error.
我在 Stack Overflow 上发现的所有相关问题都修复了一个特定的代码片段,并没有更一般地解释导致错误的原因。
These include
这些包括
- Expected '(' for function-style cast or type constructionanswer highlights several issues with the code. Which issue is actually causing the error is unclear.
- c++ Xcode expected '(' for function-style cast or type constructiondefines functions inside a main function. This seems like a clear syntax problem, but why this specific error is produced is still unclear to me.
- '(' for function-style cast or construction type Xcode error. In this last example, the OP calls a function in a way that looks very similar to a function declaration, plus they declare a function with the same name but a different signature. Based on where the error is thrown and the message from the error, it seems that the error has something to do with function declarations.
- 预期的 '(' for function-style cast or type constructionanswer 突出显示了代码的几个问题。实际上哪个问题导致了错误尚不清楚。
- c++ Xcode expected '(' for function-style cast or type construction定义了主函数内的函数。这似乎是一个明确的语法问题,但为什么会产生这个特定的错误我仍然不清楚。
- '(' for function-style cast or building type Xcode error。在最后一个例子中,OP以一种看起来非常类似于函数声明的方式调用函数,而且它们声明了一个具有相同名称但签名不同的函数。根据抛出错误的位置和错误消息,错误似乎与函数声明有关。
Can I get a documentation-style answer that translates what "function-style cast" and "type construction" mean in simple english? When does the compiler choose to throw this error instead of some other error?
我能得到一个文档式的答案,用简单的英语翻译“函数式转换”和“类型构造”的意思吗?编译器什么时候选择抛出这个错误而不是其他一些错误?
I don't want an answer that is specific to my own error, but as requested, here is my MCVE
我不想要特定于我自己的错误的答案,但根据要求,这是我的 MCVE
#include <boost/integer_traits.hpp>
class Test{
const double MAX_DEPTH_VAL = (double) boost::integer_traits<unsigned short>.const_max;
const double MIN_DEPTH_VAL = (double) boost::integer_traits<unsigned short>.const_max;
};
I was led to believe that this syntax was possible, by this answer https://stackoverflow.com/a/2738576/3303546
我被引导相信这种语法是可能的,通过这个答案https://stackoverflow.com/a/2738576/3303546
回答by M.M
This is a syntax error. Now, non-programmers or amateurs might hear the term syntax errorand equate it with a general bug. But in C++ (and other languages) it has a more specific meaning.
这是一个语法错误。现在,非程序员或业余爱好者可能会听到术语语法错误并将其等同于一般错误。但是在 C++(和其他语言)中,它具有更具体的含义。
There is a language grammarwhich is a set of rules by which the compiler, at an early stage of translation, breaks up the source code into logical groups. This is before any meaning is ascribed to those groups (that part is sometimes called semantic checking).
有一种语言语法,它是一组规则,编译器在翻译的早期阶段将源代码分解为逻辑组。这是在将任何含义归因于这些组之前(该部分有时称为语义检查)。
The syntax error you saw means that the compiler could not match up the source code to the grammar rules for C++. Precisely because it could not do this -- it's hard for the compiler to know what the programmer intended. So, syntax error messages are often guesses or don't relate to the programmer intention.
您看到的语法错误意味着编译器无法将源代码与 C++ 的语法规则匹配。正是因为它不能这样做——编译器很难知道程序员的意图。因此,语法错误消息通常是猜测或与程序员意图无关。
When you see this error, the compiler is suggesting a way of changing the code that would possibly match one of the grammar rules, but that may or may not actually be a good fix in the situation.
当您看到此错误时,编译器正在建议一种更改可能与其中一个语法规则匹配的代码的方法,但这实际上可能是也可能不是这种情况下的良好修复。
So, you can treat this sort of error just as "general syntax error", not worrying too much about the details of the error. To fix it, go back to simpler expressions that you are sure are not syntax errors, and then build up towards what you wanted to write.
因此,您可以将此类错误视为“一般语法错误”,而不必太担心错误的细节。要修复它,请返回到您确定不是语法错误的更简单的表达式,然后朝着您想要编写的方向构建。
An analogy for English language might be the sentence "I the room went of". Imagine some language translation software. This doesn't match any known sentence structure but what error message can it report? The actual suggestions probably won't help you to fix the sentence.
英语语言的类比可能是句子“我房间去了”。想象一些语言翻译软件。这与任何已知的句子结构都不匹配,但它可以报告什么错误消息?实际的建议可能无法帮助您修正句子。
In your specific example, there is a syntax error. The g++ error message is different:
在您的具体示例中,存在语法错误。g++ 错误信息是不同的:
error: expected primary-expression before '.' token
错误:'.' 之前的预期主表达式 令牌
where primary-expressionis an entry in the C++ grammar. g++ sees the .
token and assumes you mean the member access operator. But the grammar says that the left-hand operand of the member access operator must be a primary-expression(and the semantic rules say that this primary-expression denotes the object whose member you want to access).
其中primary-expression是C++ 语法中的一个条目。g++ 看到.
令牌并假设您指的是成员访问运算符。但是语法说成员访问运算符的左侧操作数必须是一个主表达式(并且语义规则说这个主表达式表示要访问其成员的对象)。
However in your actual code the left-hand side is (double) boost::integer_traits<unsigned short>
which does not match the grammar specification for primary-expression. (In fact it's a type name). The compiler can't proceed any further from here so it bails out.
但是,在您的实际代码中,左侧(double) boost::integer_traits<unsigned short>
与primary-expression的语法规范不匹配。(实际上它是一个类型名称)。编译器无法从这里继续进行下去,因此它退出了。
Your compiler also failed to match the code to any grammar rule, but it guessed you were trying to write a function-style cast or type construction.
您的编译器也未能将代码与任何语法规则匹配,但它猜测您正在尝试编写函数样式的强制转换或类型构造。
"Function-style cast" means code like int(5.0)
, so perhaps it recognized boost::integer_traits<unsigned short>
as a type-name, and it guessed that you meant boost::integer_traits<unsigned short>(const_max)
, i.e. casting some variable const_max
to that type.
“函数式类型转换”的意思是类似 的代码int(5.0)
,所以它可能识别boost::integer_traits<unsigned short>
为一个类型名称,并且它猜测您的意思是boost::integer_traits<unsigned short>(const_max)
,const_max
即将某个变量转换为该类型。
I'm not sure what your compiler means by "type construction" here.
我不确定你的编译器在这里的“类型构造”是什么意思。
NB. If you want to know how to fix the actual code in your question, I'd suggest starting a new question where you post the code and error message and ask how to fix the code.
注意。如果您想知道如何修复问题中的实际代码,我建议您开始一个新问题,在其中发布代码和错误消息并询问如何修复代码。