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
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
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: