C++ program error message

L

Latina

Hi I am doing a program overloaded operator.
I am having a few errors on it.
Error1: request for member `insertElement' in `S1set', which is of non-
class type `IntegerSet[26]'
Error2: no matching function for call to `IntegerSet::eek:perator+
(IntegerSet[26])'
Here is my program:
class IntegerSet
{
private:
bool set[26];
int element;
public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union
//Methods
IntegerSet(); //default
constructor
IntegerSet(int x[], int k); //overload
constructor
bool isValid(int)const;
void insertElement(int);
void deleteElement(int);
void setString();
void inputSet();
};
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;
}
}
bool IntegerSet::isValid(int i)const
{
return set;
}
//insert element to a set
void IntegerSet::insertElement(int element)
{
set[element]=true;
}
//delete element of a set
void IntegerSet::deleteElement(int element)
{
set[element]=false;
}
//overloaded operator + to compute the union of two sets
IntegerSet IntegerSet::eek:perator+(const IntegerSet &right)const
{
IntegerSet j;
for(int element=0; element<=25; element++)
{
if(isValid(element) || right.isValid(element))
j.insertElement(element);
}
return j;
}
int main()
{
IntegerSet run;
IntegerSet S1set[26];
IntegerSet S2set[26];
IntegerSet S3set[26];
IntegerSet Sset[26];
for(int i=2; i<=20; i+2)
S1set.insertElement(); <--Error 1
for(int k=6; k<=21; k+3)
S2set.insertElement(); <--Error 1
for(int j=3; j<=18; j+6)
S3set.insertElement(); <--Error 1
for(int z=0; z<=25; z++)
Sset.insertElement(); <--Error 1
run.inputSet();
int choice;
cout<<"\n WELCOME to the INTEGER SET PROGRAM\n";
cout<<"\n\nSelect one of these choices\n";
cout<<" 0. Create set \n";
cout<<" 1. Find Union \n";
cin>>choice;
if(choice==0)
{
int temp, ele;
int newSet[26];
cout<<"Enter how many elements you want in the set: "<<endl;
cin>>ele;
for(int i=0; i<ele; i++)
{
cout<<i+1;
cin>>temp;
newSet=temp;
}
}
else if(choice==1)
{
char a, b, c, d;
int option;
cout<<"Select one of this choices";
cout<<"a. To find the union of the set you enter and the set
'S'";
cout<<"b. To find the union of the set you enter and the set
'S1'";
cout<<"c. To find the union of the set you enter and the set
'S2'";
cout<<"d. To find the union of the set you enter and the set
'S3'";
cin>>option;
if(option=='a'||option=='A')
{
run.operator+(Sset); <--Error 2
}
else if(option=='b'||option=='B')
{
run.operator+(S1set); <--Error 2
}
else if(option=='c'||option=='C')
{
run.operator+(S2set); <--Error 2
}
else if(option=='d'||option=='D')
{
run.operator+(S3set); <--Error 2
}
}
return 0;
}

I hope some one can help me.
 
M

Michael DOUBEZ

Latina a écrit :
Hi I am doing a program overloaded operator.
I am having a few errors on it.
Error1: request for member `insertElement' in `S1set', which is of non-
class type `IntegerSet[26]'
Error2: no matching function for call to `IntegerSet::eek:perator+
(IntegerSet[26])'
Here is my program:
class IntegerSet
{
private:
bool set[26];
int element;
public:
//Operator methods.
IntegerSet operator + (const IntegerSet &)const; //Method
union
//Methods
IntegerSet(); //default
constructor
IntegerSet(int x[], int k); //overload
constructor
bool isValid(int)const;
void insertElement(int);
void deleteElement(int);
void setString();
void inputSet();
};
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;
}
}
bool IntegerSet::isValid(int i)const
{
return set;
}
//insert element to a set
void IntegerSet::insertElement(int element)
{
set[element]=true;
}
//delete element of a set
void IntegerSet::deleteElement(int element)
{
set[element]=false;
}
//overloaded operator + to compute the union of two sets
IntegerSet IntegerSet::eek:perator+(const IntegerSet &right)const
{
IntegerSet j;
for(int element=0; element<=25; element++)
{
if(isValid(element) || right.isValid(element))
j.insertElement(element);
}
return j;
}
int main()
{
IntegerSet run;
IntegerSet S1set[26];
IntegerSet S2set[26];
IntegerSet S3set[26];
IntegerSet Sset[26];
for(int i=2; i<=20; i+2)
S1set.insertElement(); <--Error 1

S1set is a table

I guess youy wanted:
IntegerSet S1set;
for(int i=2; i<=20; i+2)
S1set.insertElement(i);
for(int k=6; k<=21; k+3)
S2set.insertElement(); <--Error 1 same
for(int j=3; j<=18; j+6)
S3set.insertElement(); <--Error 1 same
for(int z=0; z<=25; z++)
Sset.insertElement(); <--Error 1 same

run.inputSet();
int choice;
cout<<"\n WELCOME to the INTEGER SET PROGRAM\n";
cout<<"\n\nSelect one of these choices\n";
cout<<" 0. Create set \n";
cout<<" 1. Find Union \n";
cin>>choice;
if(choice==0)
{
int temp, ele;
int newSet[26];
cout<<"Enter how many elements you want in the set: "<<endl;
cin>>ele;
for(int i=0; i<ele; i++)
{
cout<<i+1;
cin>>temp;
newSet=temp;
}
}
else if(choice==1)
{
char a, b, c, d;
int option;
cout<<"Select one of this choices";
cout<<"a. To find the union of the set you enter and the set
'S'";
cout<<"b. To find the union of the set you enter and the set
'S1'";
cout<<"c. To find the union of the set you enter and the set
'S2'";
cout<<"d. To find the union of the set you enter and the set
'S3'";
cin>>option;
if(option=='a'||option=='A')
{
run.operator+(Sset); <--Error 2


What do you want to do here ?
You are calling
IntegerSet IntegerSet::eek:perator+(const IntegerSet &right)const

which return a IntegerSet.

}
else if(option=='b'||option=='B')
{
run.operator+(S1set); <--Error 2
}
else if(option=='c'||option=='C')
{
run.operator+(S2set); <--Error 2
}
else if(option=='d'||option=='D')
{
run.operator+(S3set); <--Error 2
}
}
return 0;
}

I hope some one can help me.

In order to factorize development, operator+() can be defined outside
the class and using operator+=

IntegerSet& IntegerSet::eek:perator+=(const IntegerSet &right)
{
for(int element=0; element<=25; element++)
{
if(isValid(element) || right.isValid(element))
set[element]=false;
}
return j;
}


and then

IntegerSet operator+(const IntegerSet &lhs,const IntegerSet &rhs)
{
IntegerSet result(lhs);
lhs+=rhs;

return result;
}

Michael
 
M

Michael DOUBEZ

Michael DOUBEZ a écrit :
Latina a écrit :
Hi I am doing a program overloaded operator.
I am having a few errors on it.
Error1: request for member `insertElement' in `S1set', which is of non-
class type `IntegerSet[26]'
Error2: no matching function for call to `IntegerSet::eek:perator+
(IntegerSet[26])'
Here is my program:
class IntegerSet
{
private:
bool set[26];
int element;

While I am at it:

* Don't save typing by defining index 'element' at the class level. It
doesn't save anything, it cost memory, it cuts optimisation and makes
your class buggy.

Imagine
for(element=0;element<26;++element)
{
this->operator+(foo);//element is modified in operator+
}

* instead of using bool set[26], you could consider std::bitset. If
optimisation matter, do a benchmark before reimplementing it yourself.

* union method could be better described by operator|() since it is
effectively what you are doing.
//Methods
IntegerSet(); //default
constructor
IntegerSet(int x[], int k); //overload
constructor
bool isValid(int)const;
void insertElement(int);
void deleteElement(int);
void setString();
void inputSet();
};
IntegerSet set();
IntegerSet::IntegerSet()
{
for(element=0; element>=25; element++)

Here, condition should be
element<=25 or element<26 but as it is, you loop never get executed.
Same for latter loops.
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;
}
}
bool IntegerSet::isValid(int i)const
{
return set;
}
//insert element to a set
void IntegerSet::insertElement(int element)
{
set[element]=true;
}
//delete element of a set
void IntegerSet::deleteElement(int element)
{
set[element]=false;
}
//overloaded operator + to compute the union of two sets
IntegerSet IntegerSet::eek:perator+(const IntegerSet &right)const
{
IntegerSet j;
for(int element=0; element<=25; element++)
{
if(isValid(element) || right.isValid(element))
j.insertElement(element);
}
return j;
}
int main()
{
IntegerSet run;
IntegerSet S1set[26];
IntegerSet S2set[26];
IntegerSet S3set[26];
IntegerSet Sset[26];
for(int i=2; i<=20; i+2)
S1set.insertElement(); <--Error 1

S1set is a table

I guess youy wanted:
IntegerSet S1set;
for(int i=2; i<=20; i+2)
S1set.insertElement(i);
for(int k=6; k<=21; k+3)
S2set.insertElement(); <--Error 1 same
for(int j=3; j<=18; j+6)
S3set.insertElement(); <--Error 1 same
for(int z=0; z<=25; z++)
Sset.insertElement(); <--Error 1 same

run.inputSet();
int choice;
cout<<"\n WELCOME to the INTEGER SET PROGRAM\n";
cout<<"\n\nSelect one of these choices\n";
cout<<" 0. Create set \n";
cout<<" 1. Find Union \n";
cin>>choice;


if(cin>>choice)
{
//to check for valid entry - reuse it latter



Use subfunctions, it will make the code clearer
if(choice==0)
{
int temp, ele;
int newSet[26];
cout<<"Enter how many elements you want in the set: "<<endl;
cin>>ele;
for(int i=0; i<ele; i++)
{
cout<<i+1;
cin>>temp;
newSet=temp;
}
}
else if(choice==1)
{
char a, b, c, d;
int option;
cout<<"Select one of this choices";
cout<<"a. To find the union of the set you enter and the set
'S'";
cout<<"b. To find the union of the set you enter and the set
'S1'";
cout<<"c. To find the union of the set you enter and the set
'S2'";
cout<<"d. To find the union of the set you enter and the set
'S3'";
cin>>option;
if(option=='a'||option=='A')
{
run.operator+(Sset); <--Error 2


What do you want to do here ?
You are calling
IntegerSet IntegerSet::eek:perator+(const IntegerSet &right)const

which return a IntegerSet.

}
else if(option=='b'||option=='B')
{
run.operator+(S1set); <--Error 2
}
else if(option=='c'||option=='C')
{
run.operator+(S2set); <--Error 2
}
else if(option=='d'||option=='D')
{
run.operator+(S3set); <--Error 2
}
}
return 0;
}

I hope some one can help me.

In order to factorize development, operator+() can be defined outside
the class and using operator+=

IntegerSet& IntegerSet::eek:perator+=(const IntegerSet &right)
{
for(int element=0; element<=25; element++)
{
if(isValid(element) || right.isValid(element))
set[element]=false;


I mean set[element]=true;
 
J

James Kanze

Michael DOUBEZ a écrit :

[...]
* union method could be better described by operator|() since it is
effectively what you are doing.

On the other hand, it has the wrong precedence:).

My SetOfCharacter class supports both, with the "rationale" that
+ adds members to the set, and - removes them. (The "rationale"
seriously breaks down, however, when I allow * for the
intersection:).)
 

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

Error message 2
Errors 5
Error message 11
printing the union (help) 8
Printing help 2
Lexical Analysis on C++ 1
Accelerated C++: Exercise 3-2 10
C program: memory leak/ segmentation fault/ memory limit exceeded 0

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,086
Latest member
ChelseaAmi

Latest Threads

Top