模板中的运算符<<重载,未定义符号错误

时间:2019-01-03 15:26:29

标签: c++ templates operator-overloading

我的代码有些困难。我尝试了所有操作,但始终显示相同的错误。 这是代码:

def queryset(self, queryset):
    cleaned_data = self.cleaned_data

    # Search
    search = cleaned_data.get('search')
    if search:
        queryset = queryset.filter(
            Q(company_name__icontains=search) |
            Q(first_name__icontains=search) |
            Q(last_name__icontains=search) |
            Q(email__icontains=search) |
            Q(ticket_reference__icontains=search)
        )

错误是:

template <class C> class Graphe {
private:
  vector<C> sommets;
  vector<pair<pair<C,C>,int> > arretes;

public:
  Graphe();
  Graphe(vector<C> sommets, vector<pair<pair<C,C>,int> > arretes);

  void addSommet(C);
  void addArrete(pair<pair<C,C>,int>);
  int findSommet(C);
  int findArrete(pair<C,C>);
  bool deleteSommet(C);
  bool deleteArrete(pair<C,C>);
  void deleteArrete(C);
  friend ostream& operator<<(ostream& ,const Graphe<C> & );
};


template< class C >
ostream & operator << ( ostream& out ,const Graphe<C> &g ) {
  out<<"Sommets:"<<endl;
  for(int i =0;i<g.sommets.size();i++){
    out<<g.sommets[i]<<";" ;
  }
  out<<endl;
  out<<"Arretes:"<<endl;
  for(int i =0;i<g.arretes.size();i++){
    out<<"sommets: "<<g.arretes.first.first<<" "<<g.arretes.first.second<<"// poid:"<<g.arretes.second<<endl;
  }
  return out;
}

int main(){
  vector<int> v;
  v.push_back(2);
  v.push_back(3);
  v.push_back(4);
  vector<pair<pair<int,int>,int> > a;
  a.push_back(make_pair(make_pair(2,3),4));
  a.push_back(make_pair(make_pair(4,3),9));
  a.push_back(make_pair(make_pair(2,4),5));
  Graphe<int> g (v,a);
  g.addSommet(5);
  g.addArrete(make_pair(make_pair(2,5),78));
  g.deleteSommet(5);
  Graphe<int>& refg = g;
  cout<<g<<endl;
  return 0;
}

由于空间原因,我没有添加所有方法,但是当我尝试在主菜单中使用运算符<<时,会发生错误。我尝试使用Undefined symbols for architecture x86_64: "operator<<(std::1::basic_ostream<char, std::1::char_traits<char> >&, Graphe<int> const&)", referenced from: _main in dij-877f1f.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) cout<<g;,但是它总是显示错误。我也尝试过使用cout<<refg;,但是它只显示一个内存地址。

编辑:我看到了大家都链接到我的答案(overloading friend operator<< for template class),我的代码与回答者所发布的代码相同,而且当我在主体中使用提示时总是显示相同的错误。

0 个答案:

没有答案