首页 > 解决方案 > 在模板中声明友元函数时发出警告

问题描述

找不到我收到以下警告的原因:

simple_list.h:36:69: warning: friend declaration ‘std::ostream&
operator<<(std::ostream&, const list<Elem>&)’ declares a non-template
function [-Wnon-template-friend]    36 |     friend std::ostream
&operator<<(std::ostream &os, const list &lt);
      |                                                                     ^ simple_list.h:36:69: note: (if this is not what you intended, make
sure the function template has already been declared and add <> after
the function name here)

我已经在头文件中声明了friend函数,如下所示

#include <iostream>
#include <limits>

template<typename Elem>
struct Link {
  Link* succ;// successor (next) link
  Elem val;// the value
};

template<typename Elem>
class list {
  private:
    unsigned int lsize;
    Link<Elem>* first;
    Link<Elem>* last; // one beyond the last link
  public:
    class iterator;
    list();
    list &operator=(const list &obj);
    bool empty();
    unsigned int size();
    iterator begin();
    iterator end();
    iterator insert_after(iterator p, const Elem &v);
    iterator erase_after(iterator p);
   // void push_back(const Elem& v);
    void push_front(const Elem &v);
   // void pop_front();
   // void pop_back();
    Elem &front();
   // Elem& back();
    unsigned int max_size();
    friend std::ostream &operator<<(std::ostream &os, const list &lt);
    ~list();
};

标签: c++

解决方案


推荐阅读