2
 #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;
    ....
  }

my question is which constructor will be invoked in this case ? Please explain

4

2 回答 2

1

正如您在此处看到的那样,由于歧义,这将无法编译

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()
于 2013-07-21T07:49:48.090 回答
0

您对构造函数的调用不明确。编译器无法知道要调用哪个构造函数,因为其中一个构造函数包含默认参数。

相反,请尝试删除参数的默认值。

于 2013-07-21T07:52:28.523 回答