1

I'm trying to create a dynamic array of pointers to an amount of instances of a class, ElectricityBill.

My current approach is to create a dynamic array of ElectricityBills (currently with nothing), then create a single instance of an ElectricityBill, create another set of ElectricityBills sized one larger this time, copy all the instances of the old dynamic array across, add the new one at the end.

The code of what I've tried is below

// Define the dyn array
int EBcount = 0;
ElectricityBill **EB = new ElectricityBill*[EBcount];

// Create a temp instance and input the data
ElectricityBill *tempEB = new ElectricityBill;
std::cin >> *tempEB;

std::cout << *tempEB << std::endl;

// Create a new dyn array and copy the instances accross
EBcount++;
ElectricityBill **temp = new ElectricityBill*[EBcount];
for (int i = 0; i < EBcount-1; i++) {
    temp[i] = EB[i];
}

// Append the new instance at the end and delete the old array
temp[EBcount-1] = tempEB;
delete [] EB;
EB = temp;

std::cout << temp[0] << std::endl;
std::cout << EB[0] << std::endl;

and the output is

E;name;1;2;3;acc;add;1/1/2000;1/2/2000;22.721;2.2721
0x100500000
0x100500000

It's worth noting that I've overloaded the operators << and >> within the class definition. The >> operator prompts the user to enter data and stores input data within the class's private section of variables, and the << operator is built like so:

std::ostream& operator<<(std::ostream &stream, ElectricityBill &printStream) {

    stream << "E;"
    << printStream.billerName << ";"
    << printStream.billerCode << ";"
    << printStream.referenceNumber << ";"
    << printStream.accountNumber << ";"
    << printStream.accountName << ";"
    << printStream.address << ";"
    << printStream.periodStartDate.day << "/" << printStream.periodStartDate.month << "/" << printStream.periodStartDate.year << ";"
    << printStream.periodDueDate.day << "/" << printStream.periodDueDate.month << "/" << printStream.periodDueDate.year << ";"
    << printStream.amountDue << ";"
    << printStream.totalGST;

    return stream;
}

For some reason, the output is outputting memory addresses instead of the expected data. Why is this happening and what can I do to fix it?

4
  • The second parameter to operator << should be a const reference, not just a reference. Also, all of this is taken care of already by std::vector. Commented Apr 17, 2016 at 6:39
  • I would have used std::vector, but this is for a uni assignment and we aren't allowed to use it unfortunately. It's nothing but first principals here :/ Commented Apr 17, 2016 at 7:06
  • I think it's time to have universities either 1) Have you use std::vector or 2) Have you write a legitimate dynamic array class instead of throwing pointers around all over the place in a main function. At least with the latter, you get to learn something about how to properly manage pointers. Commented Apr 17, 2016 at 7:13
  • @PaulMcKenzie I wholeheartedly agree. For modern computer science courses, they really are somewhat outdated and focus too much on stuff we would rarely ever use in the real world. Yes it is good to learn some of the first principal stuff that makes up what we will be using via a library of some sort for better understanding, but in all honesty a lot of universities focus on it a little 'too' much... Commented Apr 17, 2016 at 7:35

1 Answer 1

2

Your overloaded operatator accepts a reference to an ElectricityBill, but you appear to be providing a pointer. Hence, the default implementation for << is being used, which prints the memory location. Try:

std::cout << *temp[0] << std::endl;
std::cout << *EB[0] << std::endl;
Sign up to request clarification or add additional context in comments.

1 Comment

I've been dealing with these pointers so much in the past few hours, I've gone mad and started to forget the basics and question my knowledge. Thanks for helping me keep my sanity!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.