How would constructor delegation be affected by the explicitness of the constructor

Hi, guys, I just encountered some trouble regarding constructor delegation and making a constructor explicit. My code is shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
class Sales_data
{
friend istream &read(istream &, Sales_data &);
public:
    Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n),    revenue(p*n) { }
    Sales_data(const string &s = ""): Sales_data(s, 0, 0) { }
    Sales_data(istream &is): Sales_data() {read(is, *this);}
private:
    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;
};
istream &read(istream &is, Sales_data &item)
{
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
}

Basically, I was writing a class named Sales_data which had 3 constructors and the second and third constructors used the first one to do initialization. The code above worked just fine. However, when I tried to make the second constructor explicit, it generated an error saying that "no matching function for call to 'Sales_data::Sales_data()'. Do you guys have any idea how this happened?
Last edited on
> when I tried to make the second constructor explicit, it generated an error

There is no error with either clang++ or g++
http://coliru.stacked-crooked.com/a/3fda5e9626c6d743

> when I tried to make the second constructor explicit, it generated an error

There is no error with either clang++ or g++
http://coliru.stacked-crooked.com/a/3fda5e9626c6d743

Thanks for replying, but I don't quite get what you meant. I'd appreciate it if you can elaborate it more. What exactly does it indicate if there is no error with either clang ++ or g++?
What compiler are you using?

What version of the compiler?

Perhaps your compiler is not quite C++11 con-format.


What compiler are you using?

What version of the compiler?

Perhaps your compiler is not quite C++11 con-format.

Um...GNU GCC compiler? So you are saying that if it's a C++11 con-format compilier, then this code would not generate the error that I mentioned? and there is nothing wrong with this code?
What version of the compiler?

it's a C++11 con-format compilier, then this code would not generate the error that I mentioned? and there is nothing wrong with this code?


I mean that the code compiled with my compiler, g++ version 4.8.2. If you have a version older than 4.8.0 then that C++11 feature may not be supported, since version 4.8.0 is the first version to "advertise" complete C++11 compliance.


> I'd appreciate it if you can elaborate it more.
> What exactly does it indicate if there is no error with either clang ++ or g++?

>> So you are saying that if it's a C++11 con-format compilier, then this code would not generate the error that I mentioned?
>> and there is nothing wrong with this code?

Yes.


With g++ -std=c++11 -Wall -Wextra -pedantic-errors

*** g++ 4.9 ***
g++-4.9 (GCC) 4.9.0
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ok compiled cleanly


*** g++ 4.8 ***
g++-4.8 (Ubuntu 4.8.1-2ubuntu1~12.04) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

main.cpp: In constructor 'Sales_data::Sales_data(std::istream&)':
main.cpp:14:45: error: no matching function for call to 'Sales_data::Sales_data()'
         Sales_data(istream &is): Sales_data() { read(is, *this) ; }
                                             ^
main.cpp:14:45: note: candidates are:
main.cpp:14:9: note: Sales_data::Sales_data(std::istream&)
         Sales_data(istream &is): Sales_data() { read(is, *this) ; }
         ^
main.cpp:14:9: note:   candidate expects 1 argument, 0 provided
main.cpp:10:9: note: Sales_data::Sales_data(const string&, unsigned int, double)
         Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n),    revenue(p*n) {}
         ^
main.cpp:10:9: note:   candidate expects 3 arguments, 0 provided
main.cpp:5:7: note: Sales_data::Sales_data(const Sales_data&)
 class Sales_data
       ^
main.cpp:5:7: note:   candidate expects 1 argument, 0 provided
main.cpp:5:7: note: Sales_data::Sales_data(Sales_data&&)
main.cpp:5:7: note:   candidate expects 1 argument, 0 provided
*** not ok (errors) ***

http://coliru.stacked-crooked.com/a/891a268614f6efb8
Get it, thanks!
Topic archived. No new replies allowed.