'mySize' undeclared (first use this function)

I'm getting an error (topic title) for apparently not declaring the private members. The error is found in the last function (resize function). Three errors actually, one for every private member. I don't understand why this happens, can anyone help me please? The first error is in line 141 for the DList.cpp

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
/*-- DList.h --------------------------------------------------------------
 
  This header file defines the data type List for processing lists.
  Basic operations are:
     Constructor
     Destructor
     Copy constructor
     Assignment operator
     empty:    Check if list is empty
     insert:   Insert an item
     erase:    Remove an item
     display:  Output the list
     << :      Output operator
-------------------------------------------------------------------------*/

#include <iostream>

#ifndef DLIST
#define DLIST

typedef int ElementType;
class List
{
 public:
 /******** Function Members ********/
   /***** Class constructor *****/
   List(int maxSize = 1024);
   /*----------------------------------------------------------------------
     Construct a List object.

     Precondition:  maxSize is a positive integer with default value 1024.
     Postcondition: An empty List object is constructed; myCapacity ==
         maxSize (default value 1024); myArray points to a dynamic
         array with myCapacity as its capacity; and mySize is 0.
   -----------------------------------------------------------------------*/

   /***** Class destructor *****/
   ~List();
   /*----------------------------------------------------------------------
     Destroys a List object.

     Precondition:  The life of a List object is over.
     Postcondition: The memory dynamically allocated by the constructor
         for the array pointed to by myArray has been returned to the heap.
   -----------------------------------------------------------------------*/

   /***** Copy constructor *****/
   List(const List & origList);
   /*----------------------------------------------------------------------
     Construct a copy of a List object.

     Precondition:  A copy of origList is needed; origList is a const
         reference parameter. 
     Postcondition: A copy of origList has been constructed.
   -----------------------------------------------------------------------*/

   /***** Assignment operator *****/
   const List & operator=(const List & rightHandSide);
   /*----------------------------------------------------------------------
     Assign a copy of a List object to the current object.

     Precondition: none 
     Postcondition: A copy of rightHandSide has been assigned to this
         object. A const reference to this list is returned.
   -----------------------------------------------------------------------*/

   //***** resize operation *****//
   void resize(int);
   /*---------------------------------------------------------------------------
   Change the capacity of a list
   
   Precondition: None.
   Postcondition: The list's current capacity is changed to newCapacity
          (but not less than the number of items already in the list).
   ---------------------------------------------------------------------------*/

   /***** empty operation *****/
   bool empty() const;
   //--- See Figure 6.1 for documentation

   /***** insert and erase *****/
   void insert(ElementType item, int pos);
   //--- See Figure 6.1 for documentation (replace CAPACITY with myCapacity)

   void erase(int pos);
   //--- See Figure 6.1 for documentation

   /***** output *****/
   void display(ostream & out) const;
   //--- See Figure 6.1 for documentation
   


 private:
 /******** Data Members ********/
   int mySize;                // current size of list
   int myCapacity;            // capacity of array
   ElementType * myArray;     // pointer to dynamic array

}; //--- end of List class

//------ Prototype of output operator
ostream & operator<< (ostream & out, const List & aList);

#endif 



Function Definitions
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*-- DList.cpp-----------------------------------------------------------
 
   This file implements List member functions.
-------------------------------------------------------------------------*/

#include <cassert>
#include <new>
using namespace std;

#include "DList.h"

//--- Definition of class constructor
List::List(int maxSize)
: mySize(0), myCapacity(maxSize)
{
   myArray = new(nothrow) ElementType[maxSize];
   assert(myArray != 0);
}

//--- Definition of class destructor
List::~List()
{
   delete [] myArray;
}

//--- Definition of copy constructor
List::List(const List & origList)
: mySize(origList.mySize), myCapacity(origList.myCapacity)
{
  //--- Get new array for copy
   myArray = new(nothrow) ElementType[myCapacity];

   if (myArray != 0)                 // check if memory available
      //--- Copy origList's elements into this new array
      for(int i = 0; i < mySize; i++)
         myArray[i] = origList.myArray[i];
   else
   {
      cerr << "*** Inadequate memory to allocate storage for list ***\n";
      exit(1);
   }
}

//--- Definition of assignment operator
const List & List::operator=(const List & rightHandSide)
{
   if (this != &rightHandSide)  // check that not self-assignment
   {
      //-- Allocate a new array if necessary
      if (myCapacity != rightHandSide.myCapacity) 
      {
         delete[] myArray;
         myCapacity = rightHandSide.myCapacity;
         myArray = new(nothrow) ElementType[myCapacity];

         if (myArray == 0)      // check if memory available
         {
            cerr << "*Inadequate memory to allocate stack ***\n";
            exit(1);
         }
      }
      //--- Copy rightHandSide's list elements into this new array
      mySize = rightHandSide.mySize;
      for(int i = 0; i < mySize; i++)
         myArray[i] = rightHandSide.myArray[i];
   }
   return *this;
}

//--- Definition of empty()
bool List::empty() const
{
   return mySize == 0;
}

//--- Definition of display()
void List::display(ostream & out) const
{
   for (int i = 0; i < mySize; i++)
     out << myArray[i] << "  ";
}

//--- Definition of output operator
ostream & operator<< (ostream & out, const List & aList)
{
   aList.display(out);
   return out;
}

//--- Definition of insert()
void List::insert(ElementType item, int pos)
{
   if (mySize == myCapacity)
   {
      mySize = 2 * mySize;
      resize( mySize );
   }
   if (pos < 0 || pos > mySize)
   {
      cerr << "*** Illegal location to insert -- " << pos 
           << ".  List unchanged. ***\n";
      return;
   }

   // First shift array elements right to make room for item

   for(int i = mySize; i > pos; i--)
      myArray[i] = myArray[i - 1];

   // Now insert item at position pos and increase list size  
   myArray[pos] = item;
   mySize++;
}

//--- Definition of erase()
void List::erase(int pos)
{
   if (mySize == 0)
   {
      cerr << "*** List is empty ***\n";
      return;
   }
  if (pos < 0 || pos >= mySize)
   {
      cerr << "Illegal location to delete -- " << pos
           << ".  List unchanged. ***\n";
      return;
   }

   // Shift array elements left to close the gap
   for(int i = pos; i < mySize; i++)
       myArray[i] = myArray[i + 1];

   // Decrease list size
    mySize--;
}

//--- Definition of resize()
void resize ( int newCapacity)
{
     if ( newCapacity < mySize)
     {
          newCapacity = mySize;
          }
          
     if ( newCapacity == myCapacity)
     {
          return;
          }
     
     //Allocate a new array with size newCapacity
     ElementType * newArray;
     newArray = new(nothrow) ElementType[newCapacity];
     assert ( newArray != 0);
     
     for( int i = 0; i < mySize; i++)
          newArray[i] = myArray[i];
     
     //Frees memory allocated to current (old) array
     delete [] myArray;
     
     myArray = newArray;
     
     myCapacity = newCapacity;
}

Last edited on
Don't forget the class scope on the definition.
void List::resize ( int newCapacity)
Last edited on
Thanks a lot man...how could I miss that? =S
Topic archived. No new replies allowed.