首页 文章

在ostream重载朋友功能中使用向量

提问于
浏览
-1

我有一个名为“KeyedCollection”的模板类,它包含将数据插入向量以及流出数据的函数 . 向量是私有成员函数 . 我似乎无法弄清楚如何在我的重载ostream friend函数中使用此向量中的信息 . 注意:我不能改变类的一般结构和函数参数,它们必须保持原样 . 我列出了所有类以供参考,但有问题的函数是最后一个 .

#include "stdafx.h"
#include <iostream>
#include <vector>
#include "Windows.h"
#include <string>

using namespace std;

template <class K, class T> 
class KeyedCollection {
public:
  // Create an empty collection
  KeyedCollection();

  // Return the number of objects in the collection
  int size() const;

  // Insert object of type T with a key of type K into the
  // collection using an “ignore duplicates” policy
  void insert(const K&, const T&);

  // Output data value of objects in the collection,
  // one data value per line
  friend ostream& operator<<(ostream&,
                             const KeyedCollection&);

private:
  // Insert required members here
        int objSize;
vector<T> objects;

};

template<class K, class T>
KeyedCollection<K,T>::KeyedCollection() {

objSize = 0;
vector<T> objects;
}   

template<class K, class T>
int KeyedCollection<K,T>::size() const {

    objSize = objects.size();

return objSize;
 }

template<class K, class T> 
void KeyedCollection<K,T>::insert(const K&,const T& c) {

objects.push_back(c);

}
// !!! function i am trying to define !!!
template<class K, class T>
ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {

outstream<<inst<<endl;

return outstream;
}

另外,我收到的错误是

“致命错误LNK1120:1个未解决的外部”

还有一个说

“错误LNK2019:未解析的外部符号”class std :: basic_ostream>&__ cdecl operator <<(class std :: basic_ostream>&,class KeyedCollection const&)“(?? 6 @ YAAAV?$ basic_ostream @ DU?$ char_traits @ D @std @@@ std @@ AAV01 @ ABV?$ KeyedCollection @ HVCustomer @@@@@ Z)在函数_main中引用“...

作为一个附带问题,任何想法可能是什么?

1 回答

  • 0

    cppreferenceJohannes Schaub - litb都提供了相同的方法来使其工作 .

    您希望将该模板的一个实例(在通用术语中称为“专业化”)作为朋友 . 你按照以下方式做到[...]

    首先在你的类定义之前做一个前向声明:

    template <class K, class T> class KeyedCollection;
    
    template<class K, class T>
    ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst);
    

    因为编译器从参数列表中知道模板参数是T和U,所以不必将它们放在<...>之间,因此它们可以保留为空 .

    然后让你的朋友声明,并确保在 operator<< 之后添加 <>

    template <class K, class T> 
    class KeyedCollection {
    public:
    
    // snip
    
    friend ostream& operator<< <> (ostream& outstream,const KeyedCollection<K,T>& inst);
    
    // snip
    
    };
    

    最后你可以定义它:

    template<class K, class T>
    ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {
    
    // Just an example
    for (const auto& t : inst.objects)
    {
        std::cout << t << std::endl;
    }
    
    return outstream;
    }
    

    Live Example


    或者,做Yakk建议 .

    template <class K, class T> 
    class KeyedCollection {
    public:
    
    // snip
    
    friend ostream& operator<<(ostream& outstream,const KeyedCollection<K,T>& inst) {
    
    for (const auto& t : inst.objects)
    {
        std::cout << t << std::endl;
    }
    
    return outstream;
    }
    
    // snip
    
    };
    

    Live Example

相关问题