C4717

I am trying to overload the operators for the class 'Set'. Set is a character set class. I was given the code for the program below, but not for overloading the < > or == operators. I am getting the C4717 (recursive on all paths) error when I compile, any help is appreciated.

Code:
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
// sets
#include <iostream>
using namespace std;

const int MaxSize = 100;

class Set{
public:
	int len;
	char members[MaxSize]; // this array holds the set

	/* The find() function is private because
	   it is not used outside the set class */
	int find(char ch); // find an element in the set

	// Construct a null set.
	Set() { len = 0;}

	// Return the number of elements in the set
	int getLength(){ return len;}

	void showset(); // Display the set
	bool isMember(char ch); // check for membership

	Set operator+(char ch); // add an element to the set
	Set operator-(char ch); // remove an element from the set

	Set operator+(Set ob2); // set union
	Set operator-(Set ob2); // set different

	bool operator<(Set ob2); // subset
	bool operator>(Set ob2); // superset

	bool operator==(Set ob2); // equal to
};

/* Return the index of the element
   specified by ch, or -1 if not found */
int Set::find(char ch){
	int i;

	for(i=0; i < len; i++)
		if(members[i] == ch) return i;

	return -1;
}

// Show the set
void Set::showset(){
	cout << "{ ";
	for(int i=0; i<len; i++)
		cout <<	 members[i] << ' ';
	
	cout << "}\n";
}

/* Return true if ch is a member of the set.
   Return false otherwise. */
bool Set::isMember(char ch){
	if(find(ch) != -1) return true;
	return false;
}

Set Set::operator+(char ch){
	Set newset;

	if(len == MaxSize){
		cout << "Set is full.\n";
		return *this; // return existing set
	}

	newset = *this; // duplicate existing set

	if(find(ch) == -1){ // if not found
		// add new element to newset
		newset.members[newset.len] = ch;
		newset.len++;
	}
	return newset;
}

// Remove an element from the set.
Set Set::operator-(char ch){
	Set newset;
	int i = find(ch); // i will be -1 if element not found

	// copy and compress the remaining elements
	for(int j=0; j < len; j++)
		if(j != i) newset = newset + members[j];

	return newset;
}

// set union
Set Set::operator+(Set ob2){
	Set newset = *this; // copy the first set

	// Add unique elements from second set to the first
	for(int i=0; i <ob2.len; i++)
		newset = newset + ob2.members[i];

	return newset; // return updated set
}

// Set the difference
Set Set::operator-(Set ob2){
	Set newset = *this; // copy the first set

	// Subtract elements from second set
	for(int i=0; i < ob2.len; i++)
		newset = newset - members[i];

	return newset; // return updated set
}

bool Set::operator<(Set ob2){
	Set big, small, final;
	big = *this > ob2 ? *this : ob2;
	small = *this < ob2 ? *this : ob2;

	for(int i=1; i<small.len; i++){
		for(int k=small.len-1; k>=i; k--){
			if(small.members[k]==big.members[k]) final + small.members[k];
			else continue;
		}
	}

	if(small==final) return true;
	return false;
}

bool Set::operator>(Set ob2){
 	Set big, small, final;
	big = *this > ob2 ? *this : ob2;
	small = *this < ob2 ? *this : ob2;

	for(int i=1; i<small.len; i++){
		for(int k=small.len-1; k>=i; k--){
			if(big.members[k]==small.members[k]) final + big.members[k];
			else continue;
		}
	}

	if(small==final) return true;
	return false;
}
bool Set::operator==(Set ob2){
	Set temp;
	temp = *this;

	for(int i=0; i<temp.len; i++){
		if(temp.members[i]==ob2.members[i]) continue;
		else break;
		return true;
	}

	return false;
}

// Demonstrate the Set class.
int main()
{
	// ocnstruct 10=element empty Set
	Set s1;
	Set s2;
	Set s3;

	s1 = s1 + 'A';
	s1 = s1 + 'B';
	s1 = s1 + 'C';

	cout << "s1 after adding A B C: ";
	s1.showset();
	cout << '\n';

	cout << "Testing for membership using isMember().\n";
	if(s1.isMember('B'))
		cout << "\'B\' is a member of s1.\n";
	else cout << "\'B\' is not a member of s1.\n";

	if(s1.isMember('T'))
		cout << "\'T\' is a member of s1.\n";
	else cout << "\'T\' is not a member of s1.\n";
	cout << '\n';

	s1 = s1 - 'B';
	cout << "s1 after s1 = s1 - 'B': ";
	s1.showset();

	s1 = s1 - 'C';
	cout << "s1 after s1 = s1 - 'C': ";
	s1.showset();
	cout << '\n';

	s2 = s2 + 'A';

	cout << "s2 after adding A W X: ";
	s2 = s2 + 'W';
	s2 = s2 + 'X';
	s2.showset();
	cout << '\n';

	s3 = s1 + s2;
	cout << "s3 after s3 = s1 + s2: ";
	s3.showset();

	s3 = s3 - s1;
	cout << "s3 after s3 - s1: ";
	s3.showset();
	cout << '\n';

	cout << "s2 after s2 = s2 - s2: ";
	s2 = s2 - s2; // s2 is a null set now
	s2.showset();
	cout << '\n';

	s2 = s2 + 'C'; // add ABC in reverse
	s2 = s2 + 'B';
	s2 = s2 + 'A';

	cout << "s2 after adding C B A: ";
	s2.showset();

	s1 = s1 - s1; //clears s1 and s2
	s2 = s2 - s2;

	cout << "s1 and s2 are now clear\ns1: ";
	s1.showset();
	cout << "s2: ";
	s2.showset();

	s1 = s1 + 'A';
	s1 = s1 + 'B';
	s1 = s1 + 'C';
	s1 = s1 + 'D';
	cout << "A-D added to s1, s1: ";
	s1.showset();

	s2 = s2 + 'A';
	s2 = s2 + 'B';
	s2 = s2 + 'C';
	cout << "A-C added to s2, s2: ";
	s2.showset();

	cout << "Checking to see if s1 is a subset of s2...\n";
	if(s1 < s2) cout << "s1 is a subset of s2...\n";
	else cout << "s1 is not a subset of s2...\n";

	if(s1 > s2) cout << "s1 is a superset of s2...\n";
	else cout << "s1 is not a superset of s2...\n";

	return 0;
}


Errors:
setmod.cpp
c:\program files\microsoft visual studio 9.0\vc\setmod.cpp(130) : warning C4717: 'Set::operator<' : recursive on all control paths, function will cause runtime stack overflow
c:\program files\microsoft visual studio 9.0\vc\setmod.cpp(146) : warning C4717: 'Set::operator>' : recursive on all control paths, function will cause runtime stack overflow
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

/out:setmod.exe
setmod.obj

Once again thanks for taking a look.
enduser000
Last edited on
Ok, Where you have
1
2
3
4
bool Set::operator<(Set ob2){
	Set big, small, final;
	big = *this > ob2 ? *this : ob2;
	small = *this < ob2 ? *this : ob2;


The 3rd line is re-calling the < operator. So your code goes into an infinite loop. You cannot call > from within > and you cannot call < from within < unless you have a way for the code to not go into a loop. In this case your code goes into a loop
Cool, is this legal to do? because this is what I meant, if not I'll assign it something different off the bat...
1
2
3
4
bool Set::operator>(Set ob2){
 	Set big, small, final;
	big = *this.len > ob2.len ? *this : ob2;
	small = *this.len < ob2.len ? *this : ob2;


enduser000
That looks better. You can use the < and > operators. Just not on ob2 or 'this' because it starts a recursive loop. Unless you are specifically going for that I'd avoid it.
Topic archived. No new replies allowed.