我在cpp中无法理解的错误

时间:2019-01-13 14:53:08

标签: c++ gcc

#include<iostream>
#include<algorithm>
using namespace std;

class node{
 int data;
 node *next;
public:
  void setdata(int data_received){
    this->data=data_received;
  }
  void setnext(node* next_received){
    this->next=next_received;
  }
  int getdata(){
    return data;
  }
  node* getnext(){
    return next;
  }
};
class graph{
  node vertices[5];
  int number_of_vertices=0;
public:
  graph(int nov){
    cout<<"constructor running"<<endl;
    int vertex;
    this->number_of_vertices=nov;
  for (int i = 0; i < nov; i++) {
    cin>>vertex;
    vertices[i].setdata(vertex);
    vertices[i].setnext(NULL);
  }
}
  void display_vertex_array(){
    cout<<"displaying the array"<<endl;
    for (int i = 0; i < number_of_vertices; i++) {
      cout<<vertices[i].getdata()<<" ";
    }
  }
  void add_vertex(int vertex3){
    cout<<"adding vertex  "<<vertex3<<endl;
    vertices[number_of_vertices].setdata(vertex3);
    number_of_vertices++;
  }
  void add_edge(int vertex1,int vertex2){
    /* everything else works fine except this part
    cout<<"adding an edge between  "<<vertex1<<"  and  "<<vertex2<<endl;
    node *vertex1_itr;
    node *vertex2_itr;
    vertex1_itr=find(vertices,vertices+number_of_vertices,vertex1);
    vertex2_itr=find(vertices,vertices+number_of_vertices,vertex2);
    if(vertex1_itr==(vertices+number_of_vertices)) {
      this->add_vertex(vertex1);
    }
    if(vertex2_itr==(vertices+number_of_vertices)){
      this->add_vertex(vertex2);
    }
    */
    node newnode1;
    node* newnode1_ptr=&newnode1;
    newnode1.setdata(vertex2);
    newnode1.setnext(vertices[vertex1].getnext());
    vertices[vertex1].setnext(newnode1_ptr);
    node newnode2;
    node* newnode2_ptr=&newnode2;
    newnode2.setdata(vertex1);
    newnode2.setnext(vertices[vertex2].getnext());
    vertices[vertex2].setnext(newnode2_ptr);
    }
};
int main(){
  cout<<"your list of 3 vertex"<<endl;
  graph adjacency_list(3);
  adjacency_list.display_vertex_array();
  cout<<endl;
  adjacency_list.add_edge(0,1);
  adjacency_list.add_edge(0,2);
  adjacency_list.display_vertex_array();
  cout<<endl;
  adjacency_list.add_edge(1,3);
  adjacency_list.add_edge(2,3);
  adjacency_list.add_edge(3,4);
  adjacency_list.display_vertex_array();
  cout<<endl;
  return 0;
}

错误是:

In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/predefined_ops.h:
In instantiation of 'bool
gnu_cxx::ops::_Iter_equals_val<_Value>::operator()(_Iterator) [with
_Iterator = node; _Value = const int]':  C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algo.h:120:14:
required from '_RandomAccessIterator
std::__find_if(_RandomAccessIterator, _RandomAccessIterator,
_Predicate, std::random_access_iterator_tag) [with _RandomAccessIterator = node; _Predicate = gnu_cxx::ops::_Iter_equals_val]' 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algo.h:161:23:
required from '_Iterator std::find_if(_Iterator, _Iterator,
_Predicate) [with _Iterator = node*; _Predicate = gnu_cxx::ops::_Iter_equals_val]' 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algo.h:3790:28:
required from '_IIter std::find(_IIter, _IIter, const _Tp&) [with
_IIter = node; _Tp = int]'  C:\Users\hp\Desktop\adjacency_list.cpp:52:66: required from here 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/predefined_ops.h:194:17:
error: no match for 'operator==' (operand types are 'node' and 'const
int')  { return it == _M_value; }  ^  In file included from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:67:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_iterator.h:820:5:
note: candidate: template bool gnu_cxx::operator==(const
gnu_cxx::normal_iterator<_IteratorL, _Container>&, const
gnu_cxx::normal_iterator<_IteratorR, _Container>&)  operator==(const
normal_iterator<_IteratorL, _Container>& lhs,  ^ 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_iterator.h:820:5:
note: template argument deduction/substitution failed:  In file
included from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/predefined_ops.h:194:17:
note: 'node' is not derived from 'const
gnu_cxx::normal_iterator<_IteratorL, _Container>'  { return *it ==
_M_value; }  ^  In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:67:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_iterator.h:827:5:
note: candidate: template bool gnu_cxx::operator==(const
gnu_cxx::normal_iterator<_Iterator, _Container>&, const
gnu_cxx::normal_iterator<_Iterator, _Container>&)  operator==(const
normal_iterator<_Iterator, _Container>& lhs,  ^ 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_iterator.h:827:5:
note: template argument deduction/substitution failed:  In file
included from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/predefined_ops.h:194:17:
note: 'node' is not derived from 'const
gnu_cxx::normal_iterator<_Iterator, _Container>'  { return *it ==
_M_value; }  ^  In file included from C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/x86_64-w64-mingw32/bits/c++allocator.h:33:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/allocator.h:46,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/string:41,

from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/locale_classes.h:40,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/ios_base.h:41,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:42, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ext/new_allocator.h:139:5:
note: candidate: template bool gnu_cxx::operator==(const
gnu_cxx::new_allocator<_Tp>&, const gnu_cxx::new_allocator<_Tp>&) 
operator==(const new_allocator<_Tp>&, const new_allocator<_Tp>&)  ^ 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ext/new_allocator.h:139:5:
note: template argument deduction/substitution failed:  In file
included from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/stl_algobase.h:71:0,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/char_traits.h:39,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ios:40, 
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/ostream:38,
from
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/iostream:39,
from C:\Users\hp\Desktop\adjacency_list.cpp:1: 
C:/TDM-GCC-64/lib/gcc/x86_64-w64-mingw32/5.1.0/include/c++/bits/predefined_ops.h:194:17:
note: 'node' is not derived from 'const gnu_cxx::new_allocator<_Tp>' 
{ return *__it == _M_value; }  ^

1 个答案:

答案 0 :(得分:1)

node vertices[5];
int vertex1;

vertex1_itr=find(vertices,vertices+number_of_vertices,vertex1);

问题是find希望能够比较它的第三个参数与前两个参数指定范围内的元素是否相等。换句话说,上面的代码正在将nodeint进行比较。但是在您的代码中,没有operator==占用nodeint。这就是错误告诉您的内容。

简单的答案是使用find_if并提供谓词功能。 Lambda函数对此很有用

vertex1_itr = find_if(vertices,
    vertices + number_of_vertices,
    [=](const node& node){ return node.getdata() == vertex1; });

该代码找到我的数据成员等于vertex1的第一个顶点。