简体   繁体   English

“Expected '(' for function-style cast or type construction”错误是什么意思?

[英]What does the "Expected '(' for function-style cast or type construction" error mean?

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.我收到错误“Expected '(' 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这些包括

  1. Expected '(' for function-style cast or type construction answer highlights several issues with the code. Which issue is actually causing the error is unclear. 函数样式转换或类型构造答案的预期 '('突出显示了代码的几个问题。哪个问题实际上导致了错误尚不清楚。
  2. c++ Xcode expected '(' for function-style cast or type construction defines functions inside a main function. This seems like a clear syntax problem, but why this specific error is produced is still unclear to me. c++ Xcode 预期的 '(' 用于函数式强制转换或类型构造在主函数内定义函数。这似乎是一个明显的语法问题,但为什么会产生这个特定错误对我来说仍然不清楚。
  3. '(' 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. '(' 用于函数样式转换或构造类型 Xcode 错误。在最后一个示例中,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 ,我被引导相信这种语法是可能的

This is a syntax error .这是一个语法错误 Now, non-programmers or amateurs might hear the term syntax error and equate it with a general bug.现在,非程序员或业余爱好者可能会听到术语语法错误并将其等同于一般错误。 But in C++ (and other languages) it has a more specific meaning.但在 C++(和其他语言)中,它具有更具体的含义。

There is a language grammar which 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++.您看到的语法错误意味着编译器无法将源代码与 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.因此,语法错误消息通常是猜测或与程序员的意图无关。

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-expression is an entry in the C++ grammar.其中primary-expression是 C++ 语法中的一个条目。 g++ sees the . g++ 看到. 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).但是语法说成员访问运算符的左侧操作数必须是主表达式(语义规则说这个主表达式表示您要访问其成员的对象)。

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 .但是,在您的实际代码中,左侧是(double) boost::integer_traits<unsigned short> ,它与primary-expression的语法规范不匹配。 (In fact it's a type name). (实际上它是一个类型名称)。 The compiler can't proceed any further from here so it bails out.编译器无法从这里继续进行,所以它退出了。

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) , ie 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.如果您想知道如何修复问题中的实际代码,我建议您开始一个新问题,在其中发布代码和错误消息并询问如何修复代码。

我通过在我的 Makefile 中添加-std=c++20来修复此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 函数式强制转换或类型构造需要 &#39;(&#39; - Expected '(' for function-style cast or type construction C++:“预期的 &#39;(&#39; 用于函数式强制转换或类型构造”错误 - C++: “Expected '(' for function-style cast or type construction” Error Xcode:错误:对于函数样式转换或类型构造,应为“(” - Xcode: error: expected '(' for function-style cast or type construction C ++ Xcode期望&#39;(&#39;用于函数样式的强制转换或类型构造 - c++ Xcode expected '(' for function-style cast or type construction &#39;(&#39;用于函数样式转换或构造类型Xcode错误 - '(' for function-style cast or construction type Xcode error 对于函数式强制转换或类型构造,我该如何解决这个预期的 &#39;(&#39;? - How do I fix this Expected '(' for function-style cast or type construction? G++ 命令抛出“expected '(' for function-style cast or type construction” - G++ command throws “expected '(' for function-style cast or type construction” 错误:从整数类型到指针类型的转换需要reinterpret_cast,C样式强制转换或函数样式强制转换 - Error: Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast 函数式转换语法如何工作? - How does function-style cast syntax work? 函数式强制转换与构造函数 - Function-style cast vs. constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM