Class problems; probably with overloading.

I've written a program. It doesn't work. I am hoping for just overloading errors and maybe passing arguments by value when it should be by reference, but I'm just not finding the problems!

I'm not asking for anyone to fix it for me, just for someone to show me where I've gone wrong.

Thank you.

Here are my function definitions. If you see an obvious problem with anything PLEASE let me know.
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//**************************************************************************
void operator +(list a, list b)
// Precondition:
// Postcondition: Overloads (+) operator; Adds two arrays together, as long
// as total number of elements will not exceed CAPACITY.
{
    for (int i = 0; i < b.size(); i++)
    {
        size_t sz = a.mySize;
        a.myList[sz] = b.myList[i];
        sz++;
    }
}

//**************************************************************************
ostream& operator <<(ostream& outs, list& a)
// Precondition: none.
// Postcondition: Overloads the (<<) operator.
{
    for (int i = 0; i < a.CAPACITY; i++)
    outs << a.myList[i] << endl;
}

//**************************************************************************
 bool list::operator ==(list secondList)
// Precondition: There are two lists to compare.
// Postcondition: Each list has been compared element by element until at
// least one inconsistency was found.  If none were found, the lists are
// equal.
{
    if(pos == secondList.pos)
    {
        if(mySize == secondList.mySize)
        {
            return true;
        }
    }
    else
        return false;
}


//**************************************************************************
list::list()
// Precondition: None
// Postcondition: A new list has been created, and the position set to 0.
{
    mySize = 0;
    for (pos = 0; pos < CAPACITY; pos++)
    {
        myList[pos] = 0;
    }
    // Set position back to beginning.
    pos = 0;
}

//**************************************************************************
list::list(const list& list1)
// Precondition: There is at least 1 list created already.
// Postcondition: The second list copies all values in first list.
{
    for (pos = 0; pos < mySize; pos++)
    {
        myList[pos] = list1.myList[pos];
    }
}

//**************************************************************************
void list::front()
// Precondition: None.
// Postcondition: Current position of array has been set to first position.
{
    pos = 0;
}

//**************************************************************************
void list::end()
// Precondition: Array is not empty.
// Postcondition: Position is set to the last available slot in array.
{
    if(mySize != 0)
    {
        pos = (CAPACITY -1);
    }
    else
    cout << "This is a blank array." << endl;
}

//**************************************************************************
void list::next()
// Precondition: As long as we are not at last array slot.
// Postcondition: Have advanced to next array slot.
{
    if((pos + 1) < CAPACITY)
    {
        pos++;
    }
    else
    cout << "You are already at the end of this array." << endl;
    outfile << "You are already at the end of this array." << endl;
}

//**************************************************************************
void list::prev()
// Precondition: Array has at least 2 numbers currently.
// Postcondition: Array position has been set to the previous slot.
{
    assert(mySize > 2);
    myList[pos - 1];
}

//**************************************************************************
int list::getPos()
// Precondition: None.
// Postcondition: Returns current position of array.
{
    return pos;
}

//**************************************************************************
void list::setPos(int p)
// Precondition: None.
// Postcondition: Position in array has been set to passed in number.
{
    myList[pos] = p;
}

//**************************************************************************
void list::insertBefore(value_type num)
// Precondition: List is not empty.
// Postcondition: New number has been inserted before current position.
{
    assert(mySize != 0);
    {
        myList[pos - 1] = num;
    }
}

//**************************************************************************
void list::insertAfter(value_type num)
// Precondition: Current position is not the last available position of array.
// Postcondition: Num has been inserted after the current array position.
{
    myList[pos +1] = num;
}

//**************************************************************************
void list::replace(int num)
// Precondition: None.
// Postcondition: Num has replaced value in current array position.
{
    myList[pos] = num;
}

//**************************************************************************
void list::erase()
// Precondition: None.
// Postcondition:Erase value in current position;shuffle array leftward
{
    myList[pos] = myList[pos + 1];
    // Move to the right one spot in array
    pos++;
    mySize--;
    for (int i = pos; pos < mySize; pos++)
    {
        myList[i] = myList[i + 1];
    }
}

//**************************************************************************
void list::clear()
// Precondition: There is at least 1 value in the list.
// Postcondition: List has been cleared out to zeroes.
{
    // Test precondition
    assert (mySize != 0);
    for ( int i = 0; i < CAPACITY; i++)
    {
        myList[i] = 0;
    }
}

//**************************************************************************
void list::reverse()
// Precondition: List is not empty.
// Postcondition: List has been reversed inside array.
{
    // Test precondition
    assert(mySize != 0);
    for (int i = 0; i < mySize; i++)
    {
        // Create temp variable to hold value
        int temp = myList[i];
        myList[i] = myList[mySize - 1];
        myList[mySize - 1] = temp;
        mySize--;
    }
}

//**************************************************************************
void list::swap(list swapList)
// Precondition: There are two non-empty lists.
// Postcondition: The arrays have been swapped out with each other.
{
    // Test precondition
    assert(mySize != 0 && swapList.mySize != 0);
    // Create temporary holder
    list temp;
    // Swap out current list to temp list
    for (int i = 0; i < mySize; i++)
    {
        temp.myList[i]= myList[i];
    }
    // Replace current list with other list
    for (int i = 0; i < max(mySize,swapList.mySize); i++)
    {
        myList[i] = swapList.myList[i];
    }
    // Replace second list with temp list (first list)
    for (int i = 0; i < max(mySize, temp.mySize); i++)
    {
        swapList.myList[i] = temp.myList[i];
    }
}

//**************************************************************************
bool list::is_empty()
// Precondition: None.
// Postcondition: Returns true if list is empty; false otherwise.
{
    if (mySize == 0)
    return true;
    else
    return false;
}

//**************************************************************************
int list::getElement()
// Precondition: List is not empty.
// Postcondition: Value of current array position is returned.
{
    assert(mySize != 0);
    return myList[pos];
}

//**************************************************************************
size_t list::size()
// Precondition: None.
// Postcondition: Returns size of array; could be 0 if array is empty.
{return mySize;}
Last edited on
I see a few potential problems:
1. Shouldn't operator+ and operator<< be part of class list?
2. operator+ can't return void
Those were the immediately obvious ones.

What does the compiler say? I can't compile it myself without a definition of class list.
Topic archived. No new replies allowed.