Class Troubles...

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 9
Reputation: iTsweetie is an unknown quantity at this point 
Solved Threads: 0
iTsweetie iTsweetie is offline Offline
Newbie Poster

Class Troubles...

 
0
  #1
Apr 9th, 2009
Okay so the deal is, our instructor wrote what the class is supposed to look like (will be included with the code) and also gave us the Input, Preconditions, Process, Output, and Postcondtions of each function that is supposed to be used. I'm having trouble with an insert, delete, and clear functions dealing with the array. There is no USER input, we're just supposed to test the functions within the main function. Here's the code I've written thus far. Any help would be much appreciated!! Thanks for your time.

Code:
  1. #include <iostream>
  2. using namespace std;
  3. #define DataType int
  4. #define Boolean bool
  5. #define ARRAYSIZE 0
  6.  
  7. class SeqList
  8. {
  9. private:
  10. int size;
  11. int listitem[ARRAYSIZE];
  12.  
  13.  
  14. public:
  15. SeqList(void);
  16.  
  17. int ListSize(void)const;
  18. Boolean ListEmpty(void) const;
  19. Boolean Find(DataType& item) const;
  20. DataType GetData(int pos) const;
  21.  
  22. void Insert(const DataType& item);
  23. void Delete(const DataType& item);
  24. DataType DeleteFront(void);
  25. void ClearList(void);
  26. };
  27.  
  28. SeqList::SeqList()
  29. {
  30. size = 0;
  31. }
  32.  
  33. int SeqList::ListSize() const
  34. {
  35. return size;
  36. }
  37.  
  38. Boolean SeqList::ListEmpty() const
  39. {
  40. if(size == 0)
  41. return true;
  42. else
  43. return false;
  44. }
  45.  
  46. Boolean SeqList::Find(DataType& item) const
  47. {
  48. if(item == listitem[size])
  49. {
  50. return true;
  51. }
  52. else
  53. return false;
  54. }
  55.  
  56. void SeqList::Insert(const int& item)//Input: Item to insert in the list, no preconditions, Process: //Add the item at the rear of the list., No output, Postconditions: The list has a new item; its size //is increased by one
  57. {
  58. size++;
  59. listitem[size] = item;
  60.  
  61. }
  62. DataType SeqList::GetData(int pos) const
  63. {
  64. if(pos >= size)
  65. abort();
  66. else
  67. return pos;
  68. }
  69. void SeqList::Delete(const int& item)//Input: Value to delete from the list, No preconditions,
  70. //Process: Scan the list and remove the first occurrence of the item in the list. Take no action if //the item is not in the list, no output, Post conditions: If a match occurs, the list has one fewer //items
  71. {
  72. if(item != listitem[size])
  73. {
  74. abort();
  75. }
  76. else
  77. for(int i = 0; i < size; i++)
  78. {
  79. listitem[size]=listitem[size-1];
  80. }
  81.  
  82. }
  83.  
  84. //DataType DeleteFront(void)//No input, Preconditions: List must not be empty, Process: //Remove the first item from the list, output: Return the value of the item that is removed
  85. //Post conditions: The list has one fewer items
  86. //{
  87. //**Code for delete only with the index listitem[0]
  88. //}
  89. void ClearList(void)//No input, no preconditions, process: Remove all elements from the list and //set the list size to 0
  90. {
  91.  
  92. }
  93. int main()
  94. {
  95. SeqList A;
  96. A.Insert(12);
  97.  
  98. cout << A.ListSize()<<endl;
  99. if(A.ListEmpty()) cout<< "The List Is Empty" << endl;
  100. else
  101. cout<< "The List Is Not Empty" << endl;
  102.  
  103. return 0;
  104. }

Each time I try to write the delete function, I get a runtime error. Stack around A is corrupt. Not sure what to do.

iTsweetie
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Class Troubles...

 
0
  #2
Apr 10th, 2009
You're defining your array with a size of zero. (ARRAYSIZE is 0.)

And even if you fix that, your code often refers to listitem[size], which is outside the array bounds.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 218 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC