2017-02-04 3 views
-1

Added Überschreibung für Operator < < für meine Klasse, aber ich bin in einer Funktion bekommen:Warum bekomme ich Fehler: keine Übereinstimmung für 'Operator <<' innerhalb der Funktion?

error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream’ and ‘Edge’)

cout << "Adding: " << edge;

Aber wenn ich versuche, diesen Code alles zu verwenden, wie erwartet funktioniert:

Edge edge1("A", "B", 3); 
    Edge* edge2 = new Edge("A", "B", 3); 
    cout << edge1 << endl; 
    cout << *edge2 << endl; 

Hier code:

#include <iostream> 
#include <vector> 
#include <algorithm> 

using namespace std; 

class Node { 
    public: 
    string name; 
    Node() {} 
    Node(string name):name(name) {} 

    bool operator==(const Node& other) { 
    return name == other.name; 
    } 
}; 

class Edge { 
    public: 
    Node x; 
    Node y; 
    int cost; 
    Edge(string x_name, string y_name, int cost) { 
    this->x = Node(x_name); 
    this->y = Node(y_name); 
    this->cost = cost; 
    } 
    Edge(Node x, Node y, int cost):x(x),y(y) {} 

    bool operator==(const Edge& other) { 
    return x == other.x && x == other.y && cost == other.cost; 
    } 
}; 

class Graph { 
    public: 
    vector<Edge> edges; 
    vector<Node> nodes; 

    bool has_node(Node node) { 
    return find(nodes.begin(), nodes.end(), node) != nodes.end(); 
    } 

    bool has_edge(Edge edge) { 
    return find(edges.begin(), edges.end(), edge) != edges.end(); 
    } 

    void add(string x_name, string y_name, int cost) { 
    Node x(x_name); 
    Node y(y_name); 
    bool has_not_x = !has_node(x); 
    bool has_not_y = !has_node(y); 
    if (has_not_x) { 
     nodes.push_back(x); 
    } 
    if (has_not_y) { 
     nodes.push_back(y); 
    } 
    if (has_not_x || has_not_y) { 
     add(x, y, cost); 
    } 
    } 

    void add(Node x, Node y, int cost) { 
    Edge edge(x, y, cost); 
    if (!has_edge(edge)) { 
     cout << "Adding: " << edge; 
     edges.push_back(edge); 
    } 
    } 
}; 

ostream& operator<<(ostream& os, const Node& node) 
{ 
    os << "(" << node.name << ")"; 
    return os; 
} 

ostream& operator<<(ostream& os, const Edge& edge) 
{ 
    os << edge.x << "-" << edge.cost << "-" << edge.y; 
    return os; 
} 

int main() 
{ 
    Graph* graph = new Graph(); 
    graph->add("A", "C", 1); 

    return 0; 
} 
+0

Ist dies die Reihenfolge, in youe Code? Dann versuchen Sie, den Operator zu verwenden, bevor er definiert ist. –

+2

Die versuchte Assertion tritt an einem Punkt auf, an dem der Operator '<<' noch nicht deklariert wurde. Verschieben Sie diese Definition von 'Graph :: add' außerhalb der Definition von' Graph' und fügen Sie sie nach der Definition von 'operator <<' ein. –

+0

Pete Becker, danke das funktioniert – ambi

Antwort

1

Weil der Compiler das liest Code linear, ist es nicht bewusst, dass die Überlastung von operator<<(ostream&, const Edge&) existiert noch nicht.

Wenn Sie eine Erklärung der Überlast vor der Definition der Klasse platzieren, wird der Code kompiliert:

// Place the function declaration before the class 
// to inform the compiler of the overload's existence. 
ostream& operator<<(ostream& os, const Edge& edge); 

class Graph { 
    public: 
     vector<Edge> edges; 
     vector<Node> nodes; 
// and so on... 
Verwandte Themen