Assignment Operator?

I'm having trouble on an assignment with the assignment operator. Here is where I try to implement:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
UIntSet& UIntSet::operator = (const UIntSet& s)
{
    if (this != &s) // check for self-assignment
      {
          if (size_ != s.size_)
          {
              delete [] bv_;
              bv_ = new unsigned char[size_];
              if (bv_ == 0)
              {
                  std::cerr << "Bitvector memory allocation failure -- terminating program. \n";
                  exit (EXIT_FAILURE);
              }
          }
          
          for (size_t i = 0; i < size_; i++)
              bv_[i] = s.bv_[i];
              size_ = s.size_;
      }
    return *this;
}




Here is UIntSet.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef _UINTSET_h                                                              
#define _UINTSET_h                                                              

#include <iostream>                                                             
#include "bitvect.h"                                                            

class UIntSet                                                                   
{                                                                             
    
    
public:  
    
    
    UIntSet(unsigned long = 64); // construct an empty set of specified element size range
    UIntSet(const UIntSet&); 
    ~UIntSet();
    
    void Insert(unsigned long n); // inserts n into set                         
    void Remove(unsigned longn); // removes n from set                          
    void Clear() const; // makes set empty                                      
    bool Member(unsigned long n) const; // returns true iff n is in set         
    bool Empty() const; // true iff set is empty;                               
    size_t Size() const; // returns number of elements in set                   
    size_t Range() const; // returns upper bound of range/universe [0, ub] 
    
   
    
    UIntSet& operator = (const UIntSet& s); // set = s (assignment operator)    
    UIntSet& operator += (const UIntSet& s); // set = set union s               
    UIntSet& operator *= (const UIntSet& s); // set intersection s              
    UIntSet& operator -= (const UIntSet& s); // set = set difference s   
    

    
private:                                                                      
    
    size_t size_; // the size of current set                                    
    fsu::BitVector bv_; // bit vector representing set                          
};                                                                            

// Global Operators:                                                            
UIntSet operator + (const UIntSet& s1, const UIntSet& s2); // returns s1 union s2                                                                            
UIntSet operator * (const UIntSet& s1, const UIntSet& s2); // returns s1 intersection s2 
UIntSet operator - (const UIntSet& s1, const UIntSet& s2); // returns s1 difference s2     
std::ostream& operator<< (std::ostream os, const UIntSet& s); // output operator   




#endif     




The first error I'm getting is that I can't delete [] bv_

Then for bv_ = new unsigned char[size_], it's telling me no viable overload for =....isn't that what I'm trying to do here? How can it already be there if I'm trying to create it?


I tried starting this assignment in a couple different places, but a I was trying to implement functions, I kept getting errors about operator= overload, so I started over and I'm guessing I need to start with this implementation. Any advice would be greatly appreciated, as I am lost.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef _BITVECT_CCP                                                            
#define _BITVECT_CCP                                                            

#include <iostream>                                                             
#include "bitvect.h"                                                            

namespace fsu                                                                   
{                                                                               
    
    std::ostream& operator << (std::ostream& os, const BitVector& bv)             
    {                                                                             
        for (size_t i = 0; i < bv.Size(); ++i)                                      
            os << bv.Test(i);                                                         
        return os;                                                                  
    }                                                                             
    
    //----------------------------------                                          
    //     BitVector                                                              
    //----------------------------------                                          
    
    // public methods                                                             
    
    BitVector::BitVector (size_t numbits) // constructor                          
    {                                                                             
        byteArraySize_ = (numbits + 7)/8;                                           
        byteArray_ = new unsigned char [byteArraySize_];                            
        if (byteArray_ == 0)                                                        
        {                                                                         
            std::cerr << "** BitVector memory allocation failure -- terminating pro\
            gram.\n";                                                                       
            exit (EXIT_FAILURE);                                                    
        }                                                                         
        for (size_t i = 0; i < byteArraySize_; ++i)                                 
            byteArray_[i] = 0x00;                                                     
    }                                                                             
    
    BitVector::BitVector (const BitVector& bv)  // copy constructor               
    {                                                                             
        byteArraySize_ = bv.byteArraySize_;                                         
        byteArray_ = new unsigned char [byteArraySize_];                            
        if (byteArray_ == 0)                                                        
        {                                                                         
            std::cerr << "** BitVector memory allocation failure -- terminating pro\
            gram.\n";                                                                       
            exit (EXIT_FAILURE);                                                    
        }                                                                         
        for (size_t i = 0; i < byteArraySize_; ++i)                                 
            byteArray_[i] = bv.byteArray_[i];                                         
    }                                                                             
    
    BitVector::~BitVector ()  // destructor                                       
    {                                                                             
        delete [] byteArray_;                                                       
    }                          
    BitVector& BitVector::operator = (const BitVector& bv)                        
    // assignment operator                                                        
    {                                                                             
        if (this != &bv)                                                            
        {                                                                         
            if (byteArraySize_ != bv.byteArraySize_)                                
            {                                                                     
                delete [] byteArray_;                                               
                byteArraySize_ = bv.byteArraySize_;                                 
                byteArray_ = new unsigned char [byteArraySize_];                    
                if (byteArray_ == 0)                                                
                {                                                                 
                    std::cerr << "** BitVector memory allocation failure -- termina\
                    ting program.\n";                                                               
                    exit (EXIT_FAILURE);                                            
                }                                                                 
            }                                                                     
            for (size_t i = 0; i < byteArraySize_; ++i)                             
                byteArray_[i] = bv.byteArray_[i];                                     
        }                                                                         
        return *this;                                                               
    } 
    size_t BitVector::Size() const                                                
    // return size of bitvector                                                   
    {                                                                             
        return 8 * byteArraySize_;                                                  
    }                                                                             
    
    void BitVector::Set ()                                                        
    // make all bits = 1                                                          
    {                                                                             
        for (size_t i = 0; i < byteArraySize_; ++i)                                 
            byteArray_[i] = 0xFF;                                                     
    }                                                                             
    
    void BitVector::Set (size_t index)                                            
    // make bit = 1: OR with mask                                                 
    {                                                                             
        byteArray_[ByteNumber(index)] |= Mask(index);                               
    }                                                                             
    
    void BitVector::Unset ()                                                      
    // make all bits = 0                                                          
    {                                                                             
        for (size_t i = 0; i < byteArraySize_; ++i)                                 
            byteArray_[i] = 0x00;                                                     
    }                                                                             
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    
    void BitVector::Unset (size_t index)                                          
    // make bit = 0: AND with inverted mask                                       
    {                                                                             
        byteArray_[ByteNumber(index)] &= ~ Mask(index);                             
    }                                                                             
    void BitVector::Flip ()                                                       
    // change all bit values                                                      
    {                                                                             
        for (size_t i = 0; i < byteArraySize_; ++i)                                 
            byteArray_[i] ^= 0xFF;                                                    
    }                                                                             
    
    void BitVector::Flip (size_t index)                                           
    // change bit value: XOR with mask                                            
    {                                                                             
        byteArray_[ByteNumber(index)] ^= Mask(index);                               
    }                                                                             
    
    bool BitVector::Test  (size_t index) const                                    
    // return bit value                                                           
    {                                                                             
        return 0 != (byteArray_[ByteNumber(index)] & Mask(index));                  
    }                                                                             
    
    // private methods                                                            
    
    size_t BitVector::ByteNumber (size_t index) const                             
    {                                                                             
        // return index / 8                                                         
        // shift right 3 is equivalent to, and faster than, dividing by 8           
        index = index >> 3;                                                         
        if (index >= byteArraySize_)                                                
        {                                                                         
            std::cerr << "** BitVector error: index out of range\n";                
            exit (EXIT_FAILURE);                                                    
        }                                                                         
        return index;                                                               
    }                                                                             
    
    unsigned char BitVector::Mask (size_t index)                                  
    {                                                                             
        // return mask for index % 8                                                
        // the low order 3 bits is the remainder when dividing by 8                 
        size_t shiftamount = index & (size_t)0x07;  // low order 3 bits             
        return (unsigned char)0x01 << shiftamount;                                  
    }                                                                             
    
} // namespace fsu                                                              

#endif 


BitVect.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef _BITVECT_H                                                              
#define _BITVECT_H                                                              

#include <cstdlib>                                                              

namespace fsu                                                                   
{                                                                               
    
    //----------------------------------                                          
    //     BitVector                                                              
    //----------------------------------                                          
    
    class BitVector;                                                              
    
    std::ostream& operator << (std::ostream&, const BitVector&);                  
    
    class BitVector                                                               
    {                                                                             
    public:                                                                       
        explicit BitVector  (size_t size);       // construct a BitVector with spec\
        ified size                                                                      
        BitVector  (const BitVector&);  // copy constructor                
        ~BitVector ();                  // destructor                      
        
        BitVector& operator = (const BitVector& a);  // assignment operator         
        
        size_t Size () const;              // return size of bitvector              
        
        void Set   ();                     // make all bits = 1                     
        void Set   (size_t index);         // make bit = 1                          
        void Unset ();                     // make all bits = 0                     
        void Unset (size_t index);         // make bit = 0                          
        void Flip  ();                     // change all bits                       
        void Flip  (size_t index);         // change bit                            
        bool Test  (size_t index) const;   // return bit value   
        
    private:                                                                      
        // data                                                                     
        unsigned char *      byteArray_;                                            
        size_t               byteArraySize_;                                        
        
        // methods                                                                  
        size_t               ByteNumber (size_t index) const;                       
        static unsigned char Mask       (size_t index);                             
        
    } ; // class BitVector                                                        
    
}   // namespace fsu                                                            
#endif                                                                           
Topic archived. No new replies allowed.