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.
// sets
#include <iostream>
usingnamespace std;
constint 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
booloperator<(Set ob2); // subset
booloperator>(Set ob2); // superset
booloperator==(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) returntrue;
returnfalse;
}
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];
elsecontinue;
}
}
if(small==final) returntrue;
returnfalse;
}
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];
elsecontinue;
}
}
if(small==final) returntrue;
returnfalse;
}
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;
elsebreak;
returntrue;
}
returnfalse;
}
// 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.
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
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.