错误:没有用于调用'std :: map <std :: __ cxx11 :: basic_string <char>的匹配函数

时间:2016-09-17 12:21:16

标签: c++ iterator shared-ptr stdmap directed-graph

我试图删除一般定向加权图的节点。当我删除一个节点时,我还需要删除该节点的传入和传出边缘,所以我不能只删除该节点,还必须删除附加到其他节点的边缘,这些节点之前用于链接到该节点被删除。

对于该函数,我尝试使用find()函数找到链接节点,该函数用于查找nodes_中的节点,该节点声明为:

std::map< N, std::shared_ptr<Node> > nodes_;

我尝试过originNodeOfIncomingEdge-&gt; edges_.erase(edge);但它不起作用,所以我尝试执行以下操作,以便在执行擦除之前在nodes_地图上找到节点。

auto findLinkingNode1 = nodes_.find(originNodeOfIncomingEdge);

但是,我一直收到以下错误,不确定是什么问题。

tests/Graph.tem:558:64: error: no matching function for call to ‘std::map<std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node>, std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node> > > >::find(std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node>&)’
    auto findLinkingNode1 = nodes_.find(originNodeOfIncomingEdge);
                                                                ^
In file included from /usr/local/include/c++/6.1.0/map:61:0,
                 from tests/Graph.h:19,
                 from tests/test8c.cpp:3:
/usr/local/include/c++/6.1.0/bits/stl_map.h:1079:7: note: candidate: std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = std::__cxx11::basic_string<char>; _Tp = std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node>; _Compare = std::less<std::__cxx11::basic_string<char> >; _Alloc = std::allocator<std::pair<const std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node> > >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const std::__cxx11::basic_string<char>, std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>, int>::Node> > >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = std::__cxx11::basic_string<char>]
       find(const key_type& __x)
       ^~~~

这是deleteNode的代码。

template <typename N, typename E>
void Graph<N, E>::deleteNode(const N& node) noexcept {
    auto findNode = nodes_.find(node);
    if (findNode != nodes_.end()) {

        for (auto edge: findNode->second->incomingEdges_) {

            // find the node which has incoming edges into the deleted node
            auto originNodeOfIncomingEdge = edge->dest.lock(); // origin node of incoming edge to deleted node

            auto nodeVal = originNodeOfIncomingEdge->val_;
            std::cout << "Print out value of origin node of incoming edge to deleted node: " << nodeVal << std::endl;


            // delete the edge from the node
            //originNodeOfIncomingEdge->edges_.erase(edge);


            auto findLinkingNode1 = nodes_.find(originNodeOfIncomingEdge);
            if (findLinkingNode1 == nodes_.end()) throw std::runtime_error("deleteNode: findLinkingNode1 DNE");

        }

        findNode->second.reset(); // deletes managed object of the shared_ptr
        nodes_.erase(findNode); // removes the node from the map container


    }
}

以下是我的Graph类的一些声明:

template <typename N, typename E> class Graph {

    private:
        struct Node;
        struct Edge;

        struct Node {
            N val_;
            int numEdges_;
            int numIncomingEdges_;
            std::set<std::shared_ptr<Edge>> edges_;
            std::set<std::shared_ptr<Edge>> incomingEdges_;
            Node() {}
            Node(const N x) : val_{x} { numEdges_=0; }
            void printNode(N n);
            ~Node();
            void update();
        };

        struct Edge {
            std::weak_ptr<Node> orig;
            std::weak_ptr<Node> dest;
            E val_;
            Edge(std::shared_ptr<Node> o, std::shared_ptr<Node> d, E x);
            Edge() {};
            void printEdge();
            ~Edge();
        };

2 个答案:

答案 0 :(得分:1)

这里编译器说使用std :: string类型的键构造映射,并使用Node类型的键调用find。 你能试试auto findLinkingNode1 = nodes_.find(originNodeOfIncomingEdge->val_);吗? 如果您共享void Graph<N, E>::deleteNode(const N& node) noexcept和/或Graph对象的实例,则会更好。

答案 1 :(得分:1)

该错误表示originNodeOfIncomingEdge的类型为std::shared_ptr<gdwg::Graph<std::__cxx11::basic_string<char>‌​, int>::Node>,而不是std::string

我相信这就是你想要的:

auto findLinkingNode1 = nodes_.find(nodeVal);
if (findLinkingNode1 == nodes_.end())
   // logic to handle