简体   繁体   English

C++ 重载 += 用于合并两个返回类型为 void 的包

[英]C++ Overloading += for union of two bags with return type of void

I am trying to overload the += function (union) for my class called Bag.我正在尝试为我的名为 Bag 的 class 重载 += function (联合)。 The return type is void so I cannot return a new array which was my original idea.返回类型是 void,所以我不能返回一个新数组,这是我最初的想法。 Knowing this, I tried to simply copy the new array into a vector, which is shown in the code below.知道了这一点,我尝试简单地将新数组复制到一个向量中,如下面的代码所示。 After filling bag1 and bag2, I used the overloaded function += which seems to work.填充 bag1 和 bag2 后,我使用了重载的 function += 似乎有效。 I then displayed bag1 which I assume would contain the union of the two bags but it only contains the original.然后我展示了 bag1,我认为它包含两个包的并集,但它只包含原始包。

Did I miss something important here as in not understanding the toVector() function?我是否错过了一些重要的事情,比如不理解 toVector() function?

I apologize for the long code, but I wasn't sure if I needed to omit parts of the unnecessary code related to this question.对于冗长的代码,我深表歉意,但我不确定是否需要省略与此问题相关的部分不必要代码。

I also tried to fill a third bag, bag3, to contain bag1 += bag2.我还尝试填充第三个袋子 bag3 以包含 bag1 += bag2。 But I assume I need to overload the = function to do that.但我认为我需要重载 = function 才能做到这一点。

#include <iostream>
#include <string>

#include <vector>
using namespace std;

template<class T>
class Bag
{
public:
    Bag();
    int getCurrentSize() const;
    bool isEmpty() const;
    bool add(const T& new_entry);
    bool remove(const T& an_entry);
    /**
     @post item_count_ == 0
     **/
    void clear();
    /**
     @return true if an_etry is found in items_, false otherwise
     **/
    bool contains(const T& an_entry) const;

    /**
     @return the number of times an_entry is found in items_
     **/
    int getFrequencyOf(const T& an_entry) const;

    /**
     @return a vector having the same cotntents as items_
     **/
    std::vector<T> toVector() const;

    void display() const;  // displays the output 

    void operator+=(const Bag<T>& a_bag); //Union

protected:
    static const int DEFAULT_CAPACITY = 200;  //max size of items_
    T items_[DEFAULT_CAPACITY];              // Array of bag items
    int item_count_;                         // Current count of bag items
    int getIndexOf(const T& target) const;


};
template<class T>
Bag<T>::Bag(): item_count_(0)
{
}  // end default constructor

template<class T>
int Bag<T>::getCurrentSize() const
{
    return item_count_;
}  // end getCurrentSize

template<class T>
bool Bag<T>::isEmpty() const
{
    return item_count_ == 0;
}  // end isEmpty


template<class T>
bool Bag<T>::add(const T& new_entry)
{
    bool has_room = (item_count_ < DEFAULT_CAPACITY);
    //bool notDup = items_.getFrequencyOf(new_entry) == 0; 
    if (has_room) //&& notDup) 
    {
        items_[item_count_] = new_entry;
        item_count_++;
        return true;
    }  // end if

    return false;
}  // end add

template<class T>
void Bag<T>::display() const {
    for(int x = 0; x < item_count_;x++)
        cout << items_[x] << ", "; 
}


/**
 @return true if an_etry was successfully removed from items_, false otherwise
 **/
template<class T>
bool Bag<T>::remove(const T& an_entry)
{
   int found_index = getIndexOf(an_entry);
    bool can_remove = !isEmpty() && (found_index > -1);
    if (can_remove)
    {
        item_count_--;
        items_[found_index] = items_[item_count_];
    }  // end if

    return can_remove;
}  // end remove


/**
 @post item_count_ == 0
 **/
template<class T>
void Bag<T>::clear()
{
    item_count_ = 0;
}  // end clear

template<class T>
int Bag<T>::getFrequencyOf(const T& an_entry) const
{
   int frequency = 0;
   int cun_index = 0;       // Current array index
   while (cun_index < item_count_)
   {
      if (items_[cun_index] == an_entry)
      {
         frequency++;
      }  // end if

      cun_index++;          // Increment to next entry
   }  // end while

   return frequency;
}  // end getFrequencyOf

template<class T>
bool Bag<T>::contains(const T& an_entry) const
{
    return getIndexOf(an_entry) > -1;
}  // end contains

template<class T>
std::vector<T> Bag<T>::toVector() const
{
   std::vector<T> bag_contents;
    for (int i = 0; i < item_count_; i++)
        bag_contents.push_back(items_[i]);

   return bag_contents;
}  // end toVector

// ********* PRIVATE METHODS **************//

template<class T>
void Bag<T>::operator+=(const Bag<T>& a_bag)
{
    Bag<T> uBag;
    for (int x = 0; x<item_count_; x++) {
        uBag.add(items_[x]);
    }

    for (int i = 0; i<a_bag.item_count_; i++) {
        uBag.add(a_bag.items_[i]);
    }
    //return uBag; // Since return type is void, I cannot do this 
    uBag.toVector(); 
}

main主要的

Bag<int> bag1;
bag1.add(1);
bag1.add(2);

Bag<int> bag2;
bag2.add(3);
bag2.add(4); 

bag1.display(); //works 1,2
bag2.display(); //works 3,4

bag1+=bag2; //no errors when run without the code below 

bag1.display() // no errors, returns 1,2 which is not what I wanted 

Bag<int> bag3; 
bag3 = bag1 += bag2; //error: no match for ‘operator=’ (operand types are ‘Bag’ and ‘void’)
// I assume I need to overload the = function for this to work 

// main.cpp:9:7: note: candidate: Bag& Bag::operator=(const Bag&)
 class Bag
       ^~~~~~~~
main.cpp:9:7: note:   no known conversion for argument 1 from ‘void’ to ‘const Bag&’
main.cpp:9:7: note: candidate: Bag& Bag::operator=(Bag&&)
main.cpp:9:7: note:   no known conversion for argument 1 from ‘void’ to ‘Bag&&’

Since your question is about the operator += function, I will focus on that.由于您的问题是关于operator += function,因此我将重点关注这一点。 The following should work:以下应该有效:

template<class T>
Bag<T> & Bag<T>::operator += (const Bag<T>& a_bag)
{
    for (int i = 0; i < a_bag.item_count_; i++) {
        add(a_bag.items_[i]);
    }
    return *this;
}

By convention the operator += modifies the left hand side ( this ) by adding/appending the right hand side ( a_bag ).按照惯例, operator +=通过添加/附加右侧( a_bag )来修改左侧( this )。 Hence no new object is created, but a reference to the left hand side, ie ( *this ) is returned which allows the =+ expression to be part of a larger expression, eg an argument in a function call.因此不会创建新的 object,但会返回对左侧的引用,即 ( *this ),这允许=+表达式成为更大表达式的一部分,例如 function 调用中的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM