printing the union (help)

L

Latina

Hi, I am doing a program of overloaded methods.
I want to get what is the union of the user set and S set and the user
set and S1 set.
I try it but it is not working.
Can some one help me please?

Here is part of my code:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union

};

void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}

main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet=temp;
}
IntegerSet S4set(newSet, ele);
cout<<"Your set is: ";
S4set.setString();

cout<<"\n\nSelect one of these choices:\n";
cout<<" 1. Find Union \n";
cout<<"Choice: ";
cin>>choice;

if(choice==1)
{
char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a'||option=='A')
{
unio = S4set.operator+(Sset);
}
else if(option=='b'||option=='B')
{
unio = S4set.operator+(S1set);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
}

Thanks
 
R

red floyd

Latina said:
Hi, I am doing a program of overloaded methods.
I want to get what is the union of the user set and S set and the user
set and S1 set.
I try it but it is not working.
Can some one help me please?

Define "Not working". What are you inputting? What are you getting
out? What are you expecting?

See FAQ 5.8 http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8

Post a minimal *COMPILABLE* example (see my comment below) that exhibits
the behavior in question. Tell us what you did, what you expected, and
what you got.
Here is part of my code:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union

};

This following method won't compile -- no prototype
void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}

Error. Implicit int not allowed.
main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet=temp;
}
IntegerSet S4set(newSet, ele);
cout<<"Your set is: ";
S4set.setString();

cout<<"\n\nSelect one of these choices:\n";
cout<<" 1. Find Union \n";
cout<<"Choice: ";
cin>>choice;

if(choice==1)
{
char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a'||option=='A')
{


Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.
unio = S4set.operator+(Sset);
}
else if(option=='b'||option=='B')
{

See above.
 
L

Latina

Hi, I am doing a program of overloaded methods. I am using Dev-C++
compiler.
The program first asks the user to enter a set of elements that are
stored in the IntegerSet S4set.
Then asks the user to enter 'a' to find the union of his/her set and
set S or enter 'b' find the union of his/her set and set S1.
Then should print out the union of the two sets. Which it is not doing
it, it only printing this: { }

Here is my code again:

class IntegerSet
{
private:
bool set[26];
int element;

public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //method union

};

IntegerSet set();

IntegerSet::IntegerSet()
{
for(element=0; element<=25; element++)
set[element]= false;
}

IntegerSet::IntegerSet(int x[], int k)
{
for(element=0; element<=25; element++)
set[element]= false;
for(int j=0; j<k; j++)
{
element=x[j];
set[element]= true;
}
}

void IntegerSet::insertElement(int element)
{
set[element]=true;
}

void IntegerSet::setString()
{
cout<<"{";
for(element=0; element<26; element++)
{
if(set[element]==true)
{
cout<<" "<<element<<" ";
}
}
cout<<"}";
}

main()
{
for(int i=2; i<21; i++)
{
if(i%2==0)
S1set.insertElement(i);
}
for(int z=0; z<26; z++)
{
Sset.insertElement(z);
}

int temp, ele;
int newSet[26];

cout<<"Enter how many elements you want in the set: ";
cin>>ele;
cout<<"Enter the values: \n";
for(int i=0; i<ele; i++)
{
cout<<i+1<<": ";
cin>>temp;
newSet=temp;
}
IntegerSet S4set(newSet, ele);

char a, b, option;

cout<<"\nYour set is: ";
S4set.setString();
cout<<"\nSet S: ";
Sset.setString();
cout<<"\nSet S1: ";
S1set.setString();

cout<<"\n\nSelect one of this choices to find the union of:\n";
cout<<" a. Your set and set 'S'\n";
cout<<" b. Your set and set 'S1'\n";
cout<<"Option: ";
cin>>option;

IntegerSet unio();

if(option=='a' || option=='A')
{
unio = S4set.operator+(Sset);
}
else if(option=='b' || option=='B')
{
unio = S4set.operator+(S1set);
}
cout<<"The union of the two sets is: \n";
unio.setString();
}
Thanks
 
L

Latina

IntegerSet unio();
Why not: unio = S4set + Sset; ? That's the whole point of operator
overloading.

because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?
 
R

red floyd

Latina said:
Hi, I am doing a program of overloaded methods. I am using Dev-C++
compiler.
The program first asks the user to enter a set of elements that are
stored in the IntegerSet S4set.
Then asks the user to enter 'a' to find the union of his/her set and
set S or enter 'b' find the union of his/her set and set S1.
Then should print out the union of the two sets. Which it is not doing
it, it only printing this: { }

Here is my code again:

And try yet again.

The followng methods are not defined in the class:

IntegerSet::IntegerSet(int[], int)
IntegerSet::IntegerSet();
void IntegerSet::insertElement(int);
void IntegerSet::setString();

Implicit int for main() is illegal. Main returns int. Declare it as:

int main()
 
R

red floyd

Latina said:
because it cannot convert `IntegerSet' to `IntegerSet ()()' in
assignment
but how I can correct that?

What do you mean? Show the code that generated this error.
 
L

Latina

What do you mean? Show the code that generated this error.- Hide quoted text -

- Show quoted text -

if(option=='a'||option=='A')
{
unio.S4set.operator+(Sset); <--Error here
}
else if(option=='b'||option=='B')
{
unio=S4set.operator+(S1set); <--Error here
}
 
R

red floyd

This line does not do what you think it does. You think it declares and
default initializes an IntegerSet named unio.

Wrong. It declares unio as a function returning an IntegerSet.
Kill the parens on this declaration.
 
J

James Kanze

[...]
Why not: unio = S4set + Sset; ? That's the whole point of
operator overloading.

On the other hand, unio isn't a variable, and so can't appear on
the right side of an assignment, regardless of what he puts on
the left.

Just a guess, but he probably meant to define unio as a local
variable, rather than as an external function. In which case,
the definition should have been:

IntegerSet unio ;

If he wants help, I think he really needs to start posting code
which compiles. Or doesn't, of course, if he's asking about a
compiler error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C++ program error message 4
Error message 2
Printing help 2
Errors 5
I need help 1
First time question 1
Codeforces problem 0
Crossword 14

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top