[英]Ambiguity in c++ constructor when a constructor with default argument exists
#include<iostream>
using namespace std;
class abc{
int a;
public: abc() { } //do nothing constructor
abc(int x=6){ a=x;} //constructor with default argument
};
main()
{
abc a;
....
}
我的问题是在这种情况下将调用哪个构造函数? 请解释
由于您在此处看到的歧义,这将无法编译
prog.cpp:8:7: warning: ISO C++ forbids declaration of ‘main’ with no type [-Wreturn-type]
prog.cpp: In function ‘int main()’:
prog.cpp:10:11: error: call of overloaded ‘abc()’ is ambiguous
prog.cpp:10:11: note: candidates are:
prog.cpp:6:12: note: abc::abc(int)
prog.cpp:5:12: note: abc::abc()
您对构造函数的调用不明确。 编译器无法知道要调用哪个构造函数,因为其中一个构造函数包含默认参数。
相反,尝试删除参数的默认值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.