C ++编译器错误C2751-到底是什么引起的? [英] C++ Compiler Error C2751 - What exactly causes it?
本文介绍了C ++编译器错误C2751-到底是什么引起的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理 C2751 编译器错误,不太清楚是什么原因造成的.以下小代码会产生错误:
I am wrestling with the C2751 compiler error and don't quite understand what exactly causes it. The following little code produces the error:
#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
显式调用构造函数?
Why can't I call the constructor explicitly for b2
?
推荐答案
这是最烦人的问题解析.在clang下进行编译可提供完整的诊断信息:
It's the problem of the most vexing parse. Compiling under clang gives a full diagnostic:
<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
按照编译器的建议进行修复:
Doing as the compiler suggests fixes it:
B b2 ((B (A::giveA ()))); // no error
这篇关于C ++编译器错误C2751-到底是什么引起的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文