copy constructor-error in the insert before and after, can't find the problem

I can't find why it won't output the correct info for the array. I've looked until I am ready to throw the computer out the door. I'm at my wits end trying to get this to work right. I finally got it to quit killing the compiler, but it's doing this. Does someone mine looking at it and instructing me as to what might be wrong. The problem is in the insertBefore or insertAfter function calls and I don't think that my copy constructor is right.

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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

typedef int eletype;
int const CAPACITY = 20;

ofstream outfile;
ifstream infile;

class List
{
  public:
    List();
    List(List & x);
    bool empty() const;
    void front();
    void end();
    void prev();
    void next();
    int getPos() const;
    void setPos(int);
    void insertBefore(eletype);
    void insertAfter(eletype);
    eletype getElement();
    int size() const;
    void replace(eletype);
    void erase();
    void clear();
    void reverse();
    void swap(List & x);
    friend ostream& operator <<(ostream& output, List& x);
    friend List operator +(List &x, List &y);
    List &operator =(List &x);
  private:
    int curr_pos, t_size, check;
    eletype myArray[CAPACITY];

};

bool operator ==(List &x, List &y);

List::List()
{
    curr_pos = 1;
    t_size = 0;
    check = 0;
}

List::List(List & x)
{
    x.front();

    for(int i = 1; i <= x.size(); i++)
    {
        myArray[i] = x.getElement();
        x.next();
    }

    t_size = x.size();

    curr_pos = 1;
    check = 0;
}

bool List::empty() const
{
    if (t_size == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void List::front()
{
    curr_pos = 1;
    check = 0;
}

void List::end()
{
    curr_pos = t_size;
    check = 0;
}

void List::prev()
{
   curr_pos--;
}

void List::next()
{
    curr_pos++;
}

int List::getPos() const
{
    return curr_pos;
}

void List::setPos(int x)
{
    curr_pos = x;
}

void List::insertBefore(eletype x)
{
    if (curr_pos == (t_size) && check == 0)
    {
        curr_pos = CAPACITY;
    }

    if (curr_pos == 1)
    {
        t_size--;
    }

    myArray[(curr_pos - 1)] = x;

    curr_pos--;

    if (t_size != (CAPACITY - 1))
    {
        t_size++;
    }

    check = 1;
}

void List::insertAfter(eletype x)
{
    if (curr_pos == 1 && check == 0)
    {
        curr_pos = 0;
    }

    myArray[(curr_pos + 1)] = x;

    curr_pos++;

    if (t_size != (CAPACITY - 1))
    {
        t_size++;
    }

    check = 1;
}

eletype List::getElement()
{
    if (curr_pos < 1)
    {
        curr_pos = 1;
    }
    else if (curr_pos > t_size && t_size > 0)
    {
        curr_pos = t_size;
    }

    return myArray[curr_pos];
}

int List::size() const
{
    return t_size;
}

void List::replace(eletype x)
{
    myArray[curr_pos] = x;
}

void List::erase()
{
    myArray[curr_pos] = 0;
}

void List::clear()
{
    for (int i = 1; i < CAPACITY; i++)
    {
        myArray[i] = 0;
    }
}

void List::reverse()
{
    int hold, hold2;

    hold2 = t_size;

    for(int i = 1; i <= (t_size / 2); i++)
    {
        hold = myArray[i];
        myArray[i] = myArray[hold2];
        myArray[hold2] = hold;

        hold2--;
    }
}

void List::swap(List & x)
{
    int hold;

    x.front();

    for(int i = 1; i <= t_size; i++)
    {
        hold = myArray[i];
        myArray[i] = x.getElement();
        x.replace(hold);

        x.next();
    }
}

ostream& operator <<(ostream& output, List& x)
{
    x.front();

    if (x.size() == 0)
    {
        output << "\nThe list is empty.";
    }

    for (int i = 1; i <= x.size(); i++)
    {
        output << x.getElement() << " ";
        x.next();
    }

    output << endl;

    return output;
}

List operator +(List &x, List &y)
{
    List z;

    x.front();
    y.front();

    for (int i = 1; i <= y.size(); i++)
    {
        z.replace(x.getElement() + y.getElement());

        x.next();
        z.next();
        y.next();
    }

    return z;
}

List& List::operator =(List &x)
{
    x.front();

    for (int i = 1; i <= x.size(); i++)
    {
        myArray[i] = x.getElement();

        x.next();
    }

    t_size = x.size();
}

bool operator ==(List & x, List &y)
{
    int check = 0;

    x.front();
    y.front();

    if (x.size() == y.size())
    {
        for (int i = 1; i <= x.size(); i++)
        {
            if (x.getElement() != y.getElement())
            {
                check = 1;
            }

            x.next();
            y.next();
        }
    }
    else
    {
        check = 1;
    }

    if (check == 0)
    {
        return true;
    }
    else
    {
        return false;
    }
}


Last edited on
If you can edit your post, highlight your code, then click on the <> button in the Format palette at the right side of the post, that'll format your code for the forum and make it a lot easier for others to read. :)
I'm sorry but the program was too large so I had to split the List up and the main:

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
int main()
  {
    ofstream outfile ("General List Values.txt");

  	List a,b;  int endit;

	for (int i=1;i<=20;i++)
	   a.insertAfter(i);
	cout << "List a : " << endl;
    cout << "  "  << a << endl;
	cout << "Number of elements in a - " << a.size() << endl;

	for (int i=1;i<=20;i++)
	   b.insertBefore(i);
	cout << "List b : " << endl;
    cout << "  "  <<  b << endl;
	cout << "Number of elements in b - " << b.size() << endl;

	if ( a == b )
	    cout << "List a & b are equal" << endl;
	  else
	    cout << "List a & b are Not equal" << endl;

	a.front();
	b.front();
	cout << "First elmenet in list a & b: " << a.getElement() << ", "
				       << b.getElement() << endl;
	for ( int i = 0; i < a.size(); a.next(),i++);
	for ( int i = 0; i < b.size(); b.next(),i++);
	cout << "Last elmenet in list a & b: " << a.getElement() << ", "
				     << b.getElement() << endl;
	cout << endl << endl << " Start of new stuff" << endl;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;

	a.front();
	b.front();
	endit = a.size()/2;
	for ( int i = 1; i<endit; i++)
	{  a.next();
	   b.next();
	}

	cout << "New position in Obj 'a' position = " << a.size()/2 << endl;

	for ( int i=1; i<8; i++)
	{
	   a.erase();
	   b.replace(i);
	}

	cout << "Modified Object 'a' " << endl;
    cout << "List a: " << a << endl;

	List c(b);
	cout << "Copy Constructor c(b)" << endl;
    cout << "List b : " << b << endl;
    cout << "List c : " << c << endl;

	if ( c == b )
	    cout << "List c & b are equal" << endl;
	  else
	    cout << "List c & b are Not equal" << endl;


	List e;
	e = c;
	cout << "Object 'c' assigned to Object 'e':" << endl;
    cout << "List c : " << c << endl;
    cout << "List e : " << e << endl;

    List d;
	d=a;
	d.front();
	endit = d.size()/2;
	for (int i=1; i<=endit; i++)
	{
		d.next();
		d.erase();
	}
	cout << "Results after some erases: Object d  " << endl;
    cout << "List d : " << d << endl;

	d.front();
	endit = d.size();
	for ( int i = 1; i < endit; d.next(), i++)
	{
		d.insertBefore(d.getElement()*2);
		d.next();
	}
	cout << "Results after some Replaces on d " << endl;
    cout << "List d : " << d << endl;

	a.front();
	endit = a.size();
	for ( int i = 1; i < endit; a.next(), i++)
	{
		a.insertBefore(a.getPos()+a.getElement());
		a.next();
		a.erase();
	}
	cout << "Results after some weird stuff on list a" << endl;
    cout << "List a : " << a << endl;

    List alist(b);
    alist.clear();
    for (int i=1;i<=10;i++)
	   alist.insertAfter(i);
	alist.front();
	cout << "New List alist with positions above: " << endl;
    for (int i=1;i<=10;i++) {
		cout << setw(5) << alist.getPos();
		alist.next();
	}
	cout << endl;
	alist.front();
	for (int i=1;i<=10;i++) {
		cout << setw(5) << alist.getElement();
		alist.next();
	}
	cout << endl;

    alist.reverse();
    cout << "Reverse New List alist : " << endl;
    cout << "  "  << alist << endl;

    List newa;
    for (int i=1;i<=20;i++)
	   newa.insertAfter(i*3);
    cout << "List alist and newa before swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;
    alist.swap(newa);
    cout << "List alist and newa after swap " << endl;
    cout << " " << alist << endl;
    cout << " " << newa << endl;


	cout << endl << "check out boundary conditions" << endl;
	List sq;
	cout << "number of elements in empty sq list = " << sq.size() << endl;
	sq.front();
	sq.erase();
	cout << "empty sq values " << sq << endl;
	sq.insertBefore(999);
	cout << "sq values " << sq << endl;
	sq.next(); sq.next();
	cout << "sq.getElement() = " << sq.getElement() << endl;
	cout << "sq values " << sq << endl;
	system("pause");   //This statement not needed in code blocks, just VS.
	return 0;
  }


http://www.cplusplus.com/forum/general/112111/
foo.cpp: In member function ‘List& List::operator=(List&)’:
foo.cpp:276:1: warning: no return statement in function returning non-void [-Wreturn-type]


> I can't find why it won't output the correct info for the array.
¿what should output?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void List::insertAfter(eletype x)
{
    if (curr_pos == 1 && check == 0)
    {
        curr_pos = 0;
    }

    myArray[(curr_pos + 1)] = x; //out of bound access

    curr_pos++;

    if (t_size != (CAPACITY - 1))
    {
        t_size++;
    }

    check = 1;
}
Topic archived. No new replies allowed.