C++ 编译器错误 C2751 - 究竟是什么原因造成的?

C++ Compiler Error C2751 - What exactly causes it?

我正在为 C2751 编译器错误而苦苦挣扎,不太明白究竟是什么导致了它。以下小代码会产生错误:

#include <iostream>  

class A {
public:
    A () { std::cout << "A constructed" << std::endl; };
    static A giveA () { return A (); }
};

class  B {
public:
    B (const A& a) { std::cout << "B constructed" << std::endl; }
};


int main () {

    B b1 = B (A::giveA ()); // works
    B b2 (B (A::giveA ())); // C2751
    B b3 (A::giveA ()); // works

}

编译器输出:

consoleapplication1.cpp(21): error C2751: 'A::giveA': the name of a function parameter cannot be qualified

为什么我不能为 b2 显式调用构造函数?

该特定指令不明确;按照您的说法,它可能是函数声明或变量定义。

使用额外的括号应该会有所帮助 - 它变得明确,因为你不能 declare/call 带有双括号的函数:

B b1 = B( A::giveA () ); // works
B b2(( B( A::giveA() ) )); // works for me
B b3( A::giveA() ); // works

另见此处:https://en.wikipedia.org/wiki/Most_vexing_parse

这是 most vexing parse 的问题。在 clang 下编译给出了完整的诊断:

<source>:18:17: error: parameter declarator cannot be qualified
    B b2 (B (A::giveA ())); // C2751
             ~~~^

<source>:18:10: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse]
    B b2 (B (A::giveA ())); // C2751
         ^~~~~~~~~~~~~~~~~

<source>:18:11: note: add a pair of parentheses to declare a variable
    B b2 (B (A::giveA ())); // C2751
          ^
          (              )
1 warning and 1 error generated.
Compiler exited with result code 1

按照编译器的建议进行修复:

B b2 ((B (A::giveA ()))); // no error 
B b2 (B (A::giveA ()));

即使您有 B class 的复制构造函数,您仍然会收到此错误,因为上面的行符合函数声明的语法规则。在 B(A::give()) 周围加括号可以解决您的问题。

啊,most vexing parse 的旧问题。

这行在这里

B b2 (B (A::giveA ())); // C2751

被解释为函数 b2 返回 B 并采用函数类型 B() 的名为 A::giveA 的参数。 C2751 错误是由无效名称引起的 A::giveA: 您不能使用限定名称作为参数名称。

要将临时 B 对象复制到 b2(我相信这是您的意图),您可以添加更多括号

B b2 (( B (A::giveA ()) ));

或使用大括号(只要没有构造函数接受包含可转换为类型 B 的类型的初始化列表)

B b2 {B (A::giveA())};