
June 1st, 2003, 03:02 PM
|
|
Junior Member
|
|
Join Date: May 2003
Posts: 24
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Overloading <<
Hi, i am having trouble overloading the << operator for a class that I have written. Suppose my class is def as follows:
Code:
class Set
{
public:
Set(int elem_num, int option)
{
for(int i=0; i<elem_num; i++)
{
if(option==FULL_SET)
bits.push_back(1);
else
bits.push_back(0);
}
}
Set operator || (Set set)
{
Set result;
for(int i=0; i<bits.size(); i++)
{
result.bits.push_back((bits[i] || set.bits[i]));
}
return result;
}
friend ostream &operator<<(ostream& out, const Set &set)
{
out << "{";
for(int i=0; i<set.bits.size(); i++)
{
if(i==set.bits.size()-1)
out << set.bits[i] << "}";
else
out << set.bits[i] << ", ";
}
}
private:
bit_vector bits;
};
This is the driver program. the problem is that I can't output the the AND of A and B using line "TWO". I get this error when compiling:
"no match for `Set & << ostream & (&)(ostream &)'
candidates are: class ostream & operator <<(ostream &, const Set &)"
If I compile with line "ONE", everything is fine, and I get the desired output. Does anyone know what I need to change so that it outputs correctly and I don't have to include the parentheses () ???? Thanks!!
Code:
int main(int argc,char **argv)
{
Set A(5, FULL_SET);
Set B(5, EMPTY_SET);
cout << "Set A: " << A << endl;
cout << "Set B: " << B << endl;
cout << "A && B " << (A&&B) << endl; /* ONE */
cout << "A && B" << A && B << endl; /* TWO */
return(0);
}
|