Need help with extraction operator overload

I think i'm missing something when i call the getDATA method in the List class.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
  #include <iostream>
#include <fstream>

using namespace std;

class DATA
{
    private:
    int key;
    string firstName;
    string lastName;
    float GPA;

    public:
    friend ifstream &operator >>(ifstream & ,DATA &);
    friend ostream &operator <<(ostream &,DATA);
    int getkey()
    {
        return key;
    };
};

ifstream &operator >>(ifstream &in,DATA &d)
{
    int KEY;
    string FIRST, LAST;
    float gpa;

    if( in >> KEY >> FIRST >> LAST >> gpa )
    {
        d.key = KEY;
        d.firstName = FIRST;
        d.lastName = LAST;
        d.GPA = gpa;
    }

    return in;
}

ostream &operator <<(ostream &out,DATA d)
{
    out << d.key << " " << d.firstName << " " << d.lastName << " " << d.GPA;
    return out;
}


class List
{
private:
    DATA **p;
    int y;
public:
    List();
    void insertDATA (DATA);
    DATA getDATA(DATA);
};


DATA List::getDATA(DATA d)
{
    for (int y = 0; y < 3; y++)
    {
        d = *p[y];
    }
    return d;
}

void List::insertDATA(DATA d)
{
    int i = 0;
    if (i < 10)
    {
        p[i] = new DATA(d);
        i++;
    }
}

List::List(void)
{
    p = new DATA*[10];
}


int main(int argc,char *argv[])
{
    DATA d;
    List list;
    char option;

    if (argc == 3)
    {
        ifstream in;
        in.open(argv[1]);

        while (in >> d)
        {
            list.insertDATA(d);
        }

        cout << list.getDATA(d) << "\n";
        ofstream out;
        out.open(argv[2]);
    }
    else
    {
        cout << "can't open input file and output file: put input file name then follow by output file name";
    }
}
What should getData() return? Right now it's equivalent to:
1
2
3
4
5
6
7
DATA List::getDATA(DATA d)
{
    d = *p[0];    // copy *p[0] to d, erasing whatever was in d
    d = *p[1];    // copy *p[1] to d, erasing whatever was in d again.
    d = *p[2];    // and again.
    return d;      // return d, which is the same as *p[2]
}
p is an array of pointers to DATA objects of size 10. Each of these arrays, it seems from line 61 but not 100% clear, encapsulates in turn a array of DATA object of size 3.
So to print the j'th DATA object within the i'th pointer in p you'd need *(p[i]+j) and to print all underlying DATA objects the loop would look like:
1
2
3
4
5
6
7
for (int i = 0; i < 10; ++i)
    {
        for (int j = 0; j < 3; ++j)
        {
            std::cout << *(p[i]+j);//uses the overloaded << operator for DATA
        }
    }

However dhayden makes a valid point, currently getDATA() returns just one DATA object from a 2D array of DATA objects. Think carefully about what you want this method to return

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
28
29
30
31
32
33
34
35
36
DATA List::getDATA(DATA d, int i)
{
    d = *p[i];

    return d;
}

int main(int argc,char *argv[])
{
    DATA d;
    List list;
    char option;

    if (argc == 3)
    {
        ifstream in;
        in.open(argv[1]);

        while (in >> d)
        {
            list.insertDATA(d);
        }

        for(int i = 0; i < 3; i++)
        {
            cout << list.getDATA(d, i) << "\n";
        }

        ofstream out;
        out.open(argv[2]);
    }
    else
    {
        cout << "can't open input file and output file: put input file name then follow by output file name";
    }
}

i tried both you your suggestion but it doesnt seem to fix the problem
Last edited on
Your post doesn't show exactly how you tried my suggestion so I can't say anything about your attempt. In general to print a 2D array of custom objects, here's an illustration:
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <string>
class DATA
{
    private:
    int m_key;
    std::string m_name;

    public:
    DATA (const int& key, const std::string& name)
        : m_key(key), m_name(name){}

    friend std::ostream &operator <<(std::ostream &,DATA);
};
std::ostream &operator <<(std::ostream &out,const DATA d)
{
    out << d.m_key << " " << d.m_name << "\n";
    return out;
}
int main()
{
   DATA d1 (1, "John");
   DATA d2 (2, "Jane");
   DATA d3 (3, "Other");
   DATA d4 (4, "Some");

   DATA p[2] = {d1, d2};
   DATA q[2] = {d3, d4};

   DATA* pq[2] = {p, q};

   DATA** pq_double = pq;//further declaration of pointer-to-pointer

   for (int i = 0; i < 2; ++i)
   {
       for (int j = 0; j < 2; ++j)

       {
           std::cout << *(pq[i]+j);
       }
   }
   for (int i = 0; i < 2; ++i)
   {
       for (int j = 0; j < 2; ++j)
       {
           std::cout << *(pq_double[i]+ j);
       }
   }
}

Output(in both cases)
1
2
3
4
1 John
2 Jane
3 Other
4 Some



Topic archived. No new replies allowed.