简体   繁体   English

我的函数应该调用什么?

[英]What is my function supposed to call?

So I have a class defined as such 所以我有一个这样定义的类

template<class ItemType>
class Bag : public BagInterface<ItemType>
{
 public:
  Bag();
  Bag(const ItemType &an_item); // contrusctor thats constructs for a signal item.
  int GetCurrentSize() const;
  bool IsEmpty() const;
  bool Add(const ItemType& new_entry);
  bool Remove(const ItemType& an_entry);
  void Clear();
  bool Contains(const ItemType& an_entry) const;
  int GetFrequencyOf(const ItemType& an_entry) const;
  vector<ItemType> ToVector() const; 

 private:
  int GetIndexOf(const ItemType& target) const;   
  static const int kDefaultBagSize_ = 6;  
  ItemType items_[kDefaultBagSize_]; // array of bag items
  int item_count_;                    // current count of bag items 
  int max_items_;                 // max capacity of the bag

and my professor specifically ask that we use the function 我的教授特别要求我们使用该功能

 void DisplayBag(const Bag<ItemType> &a_bag);

to display a contents in a bag, problem is that I have no idea how to get it to work. 在袋子里装东西,问题是我不知道如何使它工作。 For example, in my int main i have 例如,在我的int main中,我有

Bag<string> grabBag;
grabBag.Add(1);
Display(grabBag);

then in my display function I have. 然后在我的显示功能中。

void DisplayBag(const Bag<ItemType> &a_bag)
{
    int j = 6;
    for(int i = 0; i < j; i++)
    {
        cout << a_bag[i] << endl;
    }
}

I tried messing with this code in multiple ways and nothing works. 我尝试以多种方式弄乱此代码,但没有任何效果。 I have 我有

void DisplayBag(const Bag<ItemType> &a_bag);

Declared before my int main() and the function itself it written in the same header file of the class implementation. 在我的int main()和函数本身之前声明了它写在类实现的相同头文件中。

vector function 向量函数

template<class ItemType>
vector<ItemType> Bag<ItemType>::ToVector() const
{
  vector<ItemType> bag_contents;
  for (int i = 0; i < item_count_; i++)
    bag_contents.push_back(items_[i]);
  return bag_contents;
}  // end toVector

In order to display the contents of a Bag , the function DisplayBag must be able to find out what the contents are . 为了显示的内容Bag ,功能DisplayBag必须能够找到的内容什么。 The only function I see by which it can do that is vector<ItemType> ToVector() const; 我看到的唯一可以执行的功能是vector<ItemType> ToVector() const; . Once you have gotten a vector<ItemType> from this function you should be able to display the data by iterating through the elements of the vector<ItemType> . 从此函数获得vector<ItemType> ,您应该能够通过迭代vector<ItemType>的元素来显示数据。 (You will be able to use the [i] syntax then because vector defines operator[] .) (由于vector定义了operator[]因此您能够使用[i]语法。)

Of course in the meantime you have had to make an extra copy of everything in the Bag in a new data structure merely in order to display it. 当然,与此同时,您只是为了显示它,就必须在新数据结构中对Bag中的所有内容进行额外的复制。

I sincerely hope that the purpose of this exercise is to give you an object lesson in the consequences of bad interface design, and that your professor has plans to show you later how this interface should be written. 我真诚地希望,此次演习的目的是为了给你不好的界面设计的后果对象的教训,那你的教授有计划地告诉你以后怎么这个接口应该被写入。

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

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