order of declaration and definition

Hi,guys, I've encountered a problem regarding the order of declaration and definition and I hope that you guys can help me out. Basically, I was trying to write a class named Sales_data with a non-member class related function named read and the 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
22
23
24
25
#include <iostream>
#include <iomanip>
using namespace std;
struct Sales_data
{
    Sales_data(): bookNo("\0"), units_sold(0),revenue(0) { }
    Sales_data(const string &s): bookNo(s) { }
    Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n), revenue(p*n) { }
    Sales_data(istream &is) {read(is, *this);}
    string isbn() const {return bookNo;}
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
    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;
}

Notice here that the read function takes a Sales_data class object as one of its argument, and in the definition of class Sales_data, this read function is used in the definition of a constructor.
 
Sales_data(istream &is) {read(is, *this);}

When I tried to run this program, it generated an error saying that in
 
Sales_data(istream &is) {read(is, *this);}

'read' had not been declared, so I tried to put a declaration before the definition of class Sales_data, but this time it said that 'Sales_data' had not been declared.
Of course I could define the constructor
 
Sales_data(istream &is) {read(is, *this);}

outside of the class definition as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <iomanip>
using namespace std;
struct Sales_data
{
    Sales_data(): bookNo("\0"), units_sold(0),revenue(0) { }
    Sales_data(const string &s): bookNo(s) { }
    Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n), revenue(p*n) { }
    Sales_data(istream &);
    string isbn() const {return bookNo;}
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
    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;
}

Sales_data::Sales_data(istream &is) {read(is, *this);}


but I was trying to see whether there is a way to definite the constructor in-class, so I put the declarations of both Sales_data class and read function at the beginning and it worked out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <iomanip>
using namespace std;
struct Sales_data;
istream &read(istream &, Sales_data &);
struct Sales_data
{
    Sales_data(): bookNo("\0"), units_sold(0),revenue(0) { }
    Sales_data(const string &s): bookNo(s) { }
    Sales_data(const string &s, unsigned n, double p): bookNo(s), units_sold(n), revenue(p*n) { }
    Sales_data(istream &is) {read(is, *this);}
    string isbn() const {return bookNo;}
    Sales_data& combine(const Sales_data&);
    double avg_price() const;
    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;
}

But this thing is I have never seen a forward class declaration before and I'm not sure if it's syntatically correct, so is this the way to do it?
Yes, it's one way to do it. Another alternative could be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct Sales_data{
    //...
    Sales_data(istream &is);
    //...
};

istream &read(istream &is, Sales_data &item);

Sales_data::Sales_data(istream &is){
    //...
}

istream &read(istream &is, Sales_data &item){
    //...
}
Get it, thanks a lot!
Topic archived. No new replies allowed.