在图类中创建新边

时间:2019-05-04 17:55:53

标签: c class dictionary pointers

这是我第一次在StackOverflow中...我试图在c ++上使用Graph类。编译器在“ insert_edge”函数中向我显示了以下msj:

(最重要的部分)

||=== Build: Debug in pruebas (compiler: GNU GCC Compiler) ===|

error: no matching function for call to ‘std::map<unsigned int, ListBasic<int>::valor*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, ListBasic<int>::valor*> > >::insert(std::pair<unsigned int, ListBasic<int>::valor*>&) const’|

candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(const value_type&) [with _Key = unsigned int; _Tp = ListBasic<int>::valor*; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, ListBasic<int>::valor*> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, ListBasic<int>::valor*> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, ListBasic<int>::valor*>] <near match>|
note:   passing ‘const std::map<unsigned int, ListBasic<int>::valor*, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, ListBasic<int>::valor*> > >*’ as ‘this’ argument discards qualifiers|

note: candidate: std::pair<typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator, bool> std::map<_Key, _Tp, _Compare, _Alloc>::insert(std::map<_Key, _Tp, _Compare, _Alloc>::value_type&&) [with _Key = unsigned int; _Tp = ListBasic<int>::valor*; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, ListBasic<int>::valor*> >; typename std::_Rb_tree<_Key, std::pair<const _Key, _Tp>, std::_Select1st<std::pair<const _Key, _Tp> >, _Compare, typename __gnu_cxx::__alloc_traits<_Alloc>::rebind<std::pair<const _Key, _Tp> >::other>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, ListBasic<int>::valor*> >; std::map<_Key, _Tp, _Compare, _Alloc>::value_type = std::pair<const unsigned int, ListBasic<int>::valor*>] <near match>|

我已经考虑了3天,但我还不知道该怎么办。应该可以(以算法的方式),但是编译器讨厌我的实现:'v

重要提示:我在该课程中使用的所有课程都已经过测试...

我只是使用类型替换了代码,以查看是否发生了某些事情,就像这样(但是编译器始终显示相同的错误):

    typename ListBasic<C>::valor* puntCosto = new typename ListBasic<C>::valor;
    puntCosto->val = costo;

    this->listaCostos.insert(puntCosto);

    pair< unsigned int , typename ListBasic<C>::valor* > p1(w->first , puntCosto);
    pair< unsigned int , typename ListBasic<C>::valor* > p2(v->first , puntCosto);

    v->second.insert(p1);
    w->second.insert(p2);

这是我的Graph实现的一部分:

    #include <map>
    #include <list>

    #include "Tripla.h"//can content 3 values
    #include "listBasic.h"//a list of structs "valor", used for containing the costs of the edges

    /*
        struct valor
        {
          T val; //content of the node
          valor* sig;//pointer to the next node
          valor* ant;//pointer to the previous node
        };
    */

    template<typename C>
    Class Graph{
    private:

    ListBasic<C> listaCostos; /// contains all the costs, to don't repeat the same value for each edge

    typedef typename ListBasic<C>::valor* iterator;
    typedef map< unsigned int , iterator> listAdy; //my "adjacentList" 1 for each node

    map< unsigned int , listAdy > vertices;//is my "list" of nodes
    };

当在两个节点(u,v)之间建立链接时,该类将同时插入(u,v)和(v,u)的两个边,然后使它们指向ListBasic listaCostos中的相同成本。

我要修复的代码如下:

    template <typename C>
    bool GrafoNDV<C>::insert_edge(int orig, int dest, const C & cost)
    {
    typename map< unsigned int , listAdy >::const_iterator v = this->vertices.find((unsigned int)origen);
    typename map< unsigned int , listAdy >::const_iterator w = this->vertices.find((unsigned int)destino);

    ...//checking for the existance of v and w. And, the edges(v,w)(w,v) mustn't exist.

    iterator puntCosto = new typename ListBasic<C>::valor;
    puntCosto->val = costo;

    this->listaCostos.insert(puntCosto);

    v->second.insert(pair< unsigned int , iterator >(w->first , puntCosto));

    w->second.insert(pair< unsigned int , typename iterator >(v->first , puntCosto));
   ...
   }

您正在帮助未来的“系统工程师”。因此,非常感谢您尝试帮助我!!! (?

0 个答案:

没有答案