| | |
Help with an overloaded operator
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hello,
I have coded an overloaded += operator and it is giving me trouble. basic function of the operator is to add data to dynamically allocated class array which has 3 member variables.
I have managed to get the first part of the function to work where if size (the current number of objects) is less than arraySize (total number of object that can be stored), then add the new data to size.
However once size is equal to arraySize my program crashes when it should run the second if statement instead. Can you please help me by pointing out my mistake so I can correct it.
Thanks.
I have coded an overloaded += operator and it is giving me trouble. basic function of the operator is to add data to dynamically allocated class array which has 3 member variables.
C++ Syntax (Toggle Plain Text)
ie. savings[i].accountNumber; savings[i].balance; savings[i].customer;
However once size is equal to arraySize my program crashes when it should run the second if statement instead. Can you please help me by pointing out my mistake so I can correct it.
Thanks.
C++ Syntax (Toggle Plain Text)
const char* Bank::operator+=(const char data[]){ const int len = arraySize; Account *temp; temp = NULL; temp = new Account[len]; int bal; char anum[15 + 1]; char cust[315 + 1]; int k = 0; sscanf(data,"%15[^,],%d,%315[^;];", anum, &bal, cust); int value = strlen(cust); cust[value] = ';'; cust[value + 1] = '\0'; if(size < arraySize){ cout << "(size < arraySize)" << endl; int x = size + 1; strcpy(savings[x].accountNumber, anum); savings[x].balance = bal; strcpy(savings[x].customer, cust); size++; cout << size << '\t' << arraySize << endl; } if(size == arraySize){ cout << "Start " << endl; int j = 0; while(j < arraySize){ strcpy(temp[j].accountNumber, savings[j].accountNumber); temp[j].balance = savings[j].balance; strcpy(temp[j].customer, savings[j].customer); j++; } delete [] savings; savings = new Account[arraySize + 10]; while(k <= arraySize + 10){ strcpy(savings[k].accountNumber, temp[k].accountNumber); savings[k].balance = temp[k].balance; strcpy(savings[k].customer, temp[k].customer); k++; } delete [] temp; int i = size + 1; strcpy(savings[i].accountNumber, anum); savings[i].balance = bal; strcpy(savings[i].customer, cust); size++; arraySize = arraySize + 10; cout << size << '\t' << arraySize << endl; } return data; }
And she said "Let there be light" and on the seveth day Windows booted.
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
And the crowds screamed in terror and cowered in fear for Microsoft had approached.
From the testament of 10011101
•
•
Join Date: Oct 2008
Posts: 40
Reputation:
Solved Threads: 6
•
•
•
•
Hello,
I have coded an overloaded += operator and it is giving me trouble. basic function of the operator is to add data to dynamically allocated class array which has 3 member variables.
I have managed to get the first part of the function to work where if size (the current number of objects) is less than arraySize (total number of object that can be stored), then add the new data to size.C++ Syntax (Toggle Plain Text)
ie. savings[i].accountNumber; savings[i].balance; savings[i].customer;
However once size is equal to arraySize my program crashes when it should run the second if statement instead. Can you please help me by pointing out my mistake so I can correct it.
Thanks.
C++ Syntax (Toggle Plain Text)
const char* Bank::operator+=(const char data[]){ const int len = arraySize; Account *temp; temp = NULL; temp = new Account[len]; int bal; char anum[15 + 1]; char cust[315 + 1]; int k = 0; sscanf(data,"%15[^,],%d,%315[^;];", anum, &bal, cust); int value = strlen(cust); cust[value] = ';'; cust[value + 1] = '\0'; if(size < arraySize){ cout << "(size < arraySize)" << endl; int x = size + 1; strcpy(savings[x].accountNumber, anum); savings[x].balance = bal; strcpy(savings[x].customer, cust); size++; cout << size << '\t' << arraySize << endl; } if(size == arraySize){ cout << "Start " << endl; int j = 0; while(j < arraySize){ strcpy(temp[j].accountNumber, savings[j].accountNumber); temp[j].balance = savings[j].balance; strcpy(temp[j].customer, savings[j].customer); j++; } delete [] savings; savings = new Account[arraySize + 10]; while(k <= arraySize + 10){ strcpy(savings[k].accountNumber, temp[k].accountNumber); savings[k].balance = temp[k].balance; strcpy(savings[k].customer, temp[k].customer); k++; } delete [] temp; int i = size + 1; strcpy(savings[i].accountNumber, anum); savings[i].balance = bal; strcpy(savings[i].customer, cust); size++; arraySize = arraySize + 10; cout << size << '\t' << arraySize << endl; } return data; }
c++ Syntax (Toggle Plain Text)
savings = new Account[arraySize + 10]; while(k <= arraySize + 10){ strcpy(savings[k].accountNumber, temp[k].accountNumber); savings[k].balance = temp[k].balance; strcpy(savings[k].customer, temp[k].customer); k++; }
assume 'arraySize' = 5 so savings is an array of length 15. say savings[15], which means savings can go from savings[0]-savings[14]. In the while loop you have k<=15, which means k=15 is valid value, so you try to access savings[15] which is trying to access undefined memory.
thanks
-chandra
-chandra
![]() |
Similar Threads
- Overloaded + operator not working correctly. (C++)
- Operator Overloading Question (C++)
- Conversion Constructor and overloaded operator= help (C++)
- Overloading assignment operator (C++)
- substitute of sizeof operator (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
Other Threads in the C++ Forum
- Previous Thread: wxWidget
- Next Thread: C++ Performance Tips
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





