簡體   English   中英

C++泛型方法給出“沒有匹配的調用函數”錯誤

[英]C++ generic Method giving 'no matching function for call' error

這是我第一次用 C++ 編碼,我是 C++ 泛型的新手。 我正在嘗試制作一個基本上表示圖形的小程序,然后使用廣度優先搜索遍歷該圖形。 我已經制作了我認為是通用圖形和通用 BFS 方法的東西。 我已經包含了所有代碼,以防我遺漏了什么。

這是確切的錯誤:

no matching function for call to 'bfs(const char [4], Graph<std::__cxx11::basic_string<char> >&)
  bfs("One", graph2);

這是代碼:

圖.h:

#ifndef GRAPH_H
#define GRAPH_H

#include <map>
#include <iostream>
#include <list>
#include <set>

using namespace std;

template <class T>
class Graph{

    private:
        map<T, set<T>> adj_list;

    public:
        Graph();
        void addVertex(T val, set<T> edges);
        set<T> getNeighbours(T vertex);
};

#endif

圖表.cpp:

#include <iostream>
#include "Graph.h"

#include <string>

using namespace std;

template <class T>
Graph<T>:: Graph(){
    ;
}

// add vertex method
template <class T>
void Graph<T>:: addVertex(T val, set<T> edges){
    //if the vertex is already created, the add the list of connections
    //to that vertex, otherwise create a new vertex and create a new set
    //of connections for the vertex
    class map<T, set<T>> :: iterator it = adj_list.find(val);
    if (it != adj_list.end()){
        it->second.insert(edges.begin(), edges.end());
    }
    else{
        adj_list.insert({val, edges});
    }

    // for all the connections, add the vertex to their corresponding
    // connection set
    for (T elem: edges){
        class map<T, set<T>> :: iterator it1 = adj_list.find(elem);
        if (it1 != adj_list.end()){
            it1->second.insert(val);
        }
        else{
            adj_list.insert({elem, {val}});
        }
    }
}

//get neighbours of given vertex
template <class T>
set<T> Graph<T>:: getNeighbours(T vertex){
    return adj_list[vertex];
}

//override << operator. Display the adjacency list
template <class T>
inline ostream& operator<<(ostream& out, const Graph<T>& H){
    out << "{\n";
    for (class map<T, set<T>>::const_iterator it = H.adj_list.begin(), end = H.adj_list.end(); it != end;++it){
        out << to_string(it->first) + " : ";
        for (class set<T>::const_iterator lit = it->second.begin(), lend = it->second.end(); lit!= lend; ++lit){
                if (distance(next(lit), lend) == 0){
                    out << to_string(*lit);
                }
                else{
                    out << to_string(*lit) + ", ";
                }
        }
        out << "\n";
    }

    out << "}" << endl;
    return out;
}

主.cpp :

#include <iostream>
#include "Graph.h"
#include "Graph.cpp"
#include <queue>

using namespace std;

template <class T> 
void bfs(T vertex, Graph<T> graph){
    // queue for bfs and set for checking if nodes have been visited
    queue<T> myQueue; 
    set<T> visited;

    //enqueue the vertex
    myQueue.push(vertex);

    //while there are still nodes to be visited
    while (!myQueue.empty()){

        //get the node to be searched next and add it to visited
        T curr = myQueue.front();
        myQueue.pop();
        visited.insert(curr);
        cout << "Visiting node:" << curr << endl;

        //add all the nodes neighbours to the queue
        for (T elem: graph.getNeighbours(curr)){
            class set<T> :: iterator it = visited.find(elem);

            //if the neighbours of the current node have been visitted then dont add to queue
            if (it == visited.end()){
                myQueue.push(elem);
            }
        }
    }
}


int main(){
    //initialize graph
    Graph<int> graph1;

    //create simple tree
    //          1
    //        /   \
    //       2     3
    //      / \   / \
    //     4   5 6   7
    set<int> connections1 = {2, 3};
    set<int> connections2 = {4, 5};
    set<int> connections3 = {6, 7};

    graph1.addVertex(1, connections1);
    graph1.addVertex(2, connections2);
    graph1.addVertex(3, connections3);

    // perfrom BFS
    bfs(1, graph1);

    Graph<string> graph2;
    set<string> connectionsS1 = {"Two", "Three"};
    set<string> connectionsS2 = {"Four", "Five"};
    set<string> connectionsS3 = {"Six", "Seven"};

    graph2.addVertex("One", connectionsS1);
    graph2.addVertex("Two", connectionsS2);
    graph2.addVertex("Three", connectionsS3);

    bfs("One", graph2);

    return 0;
}

編譯器從你傳遞給它的參數中推導出bfs的模板類型。 由於第一個參數是const char[4]類型,它選擇它作為模板類型,然后失敗,因為Graph<string>不能轉換為Graph<const char[4]>

您可以將字符串作為第一個參數傳遞:

bfs(string("One"), graph2);

或者明確告訴編譯器模板類型:

bfs<string>("One", graph2);

我認為bfs也應該通過引用來獲取圖形而不是復制它們?

void bfs(T vertex, const Graph<T>& graph)

您不應該包含 .cpp 文件,我猜您這樣做是為了避免由於為什么模板只能在頭文件中實現而導致的鏈接器錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM