Thread: Overloading <<

    #1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep 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);
      
    }
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    24
    Rep Power
    0

    OOPS i meant to include the && function


    Oops, sorry I had written a || and a && function, and included the wrong one by mistake, but it's basically the same as the || except that it ands the values instead of oring them.....SOrry about that

IMN logo majestic logo threadwatch logo seochat tools logo