【问题标题】:Explicit constructor with all default arguments can't be called from another constructor of the same class不能从同一类的另一个构造函数调用具有所有默认参数的显式构造函数
【发布时间】:2015-02-10 16:36:12
【问题描述】:

为什么不能从同一个类的另一个构造函数中调用具有所有默认参数的显式构造函数?

 #include <iostream>
 #include <string>


 class A {
 public:
   explicit A(int a = 1, int b = 2) :
     a_(a),
     b_(b) {}

   A(std::string s)
     : A() {
       std::cout << s;
   }   

   int a_;
   int b_;
 };

 int main() {
   A a("!");
   std::cout << a.a_;
 }

g++ -v

gcc 版本 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

g++ -o out -std=c++11 main.cpp

错误:main.cpp:12:13:错误:没有匹配函数调用‘A::A()’

【问题讨论】:

标签: c++ constructor explicit


【解决方案1】:

看起来像是 4.9.0 中修复的 gcc 错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58255

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-12
    • 2012-06-22
    • 2015-09-22
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多