Brauche ein Vektor dessen Elemente Zeiger sind



  • Hallo

    Ich habe folgendes Problem. Ich benötige eine grosse Anzahl an Zeigern von Typ Graph. Da ich aber nicht weiss wieviel davon, will ich einen Vektor benutzen. Also deklariere ich ein Vektor:

    Graph* input; 
    vector<Graph*> g(i);
    

    Sobald ich aber einen Zeiger des gleichen Typen in diesen Vektor speichern möchte, bekomme ich vom Compiler eine Fehlermeldung

    g.at(j) = input;
    

    Was mache ich falsch.

    Gruss kossi



  • Eigentlich nix, bis auf im falschen Forum zu posten (ein freundlicher Moderator wird sich sicher verschieben).

    Eine Angabe der Fehlermeldung, sowie ein bißchen mehr Quelltext (compilierbares Minimalbeispiel) wären nicht schlecht.



  • Tja, den Text der Fehlermeldung hast du ja geschickterweise weggelassen.
    🙄

    Syntaktisch siehts richtig aus. Bist du sicher das du nicht von einen
    Laufzeitfehler sprichst.



  • Ok 🙂 dann fange ich einfach mal von ganz vorne an. Die Aufgabe lautet : Edmonds Branching Algorithm zu implementieren. Dabei habe ich folgendes Programmgerüst zu Verfügung.

    #ifndef GRAPH_HPP
    #define GRAPH_HPP
    
    #include <iostream>
    #include <vector>
    
    typedef unsigned int NodeId;
    typedef unsigned int EdgeId;
    typedef long long    Weight;
    
    class Graph
    {
    public:
    
       class Node
       {
       public:
          void add_incoming(EdgeId edge);
          void add_outgoing(EdgeId edge);
    
          std::vector<EdgeId> const & in_edges() const;
          std::vector<EdgeId> const & out_edges() const;
    
       private:
          std::vector<EdgeId> incoming_;
          std::vector<EdgeId> outgoing_;
       };
    
       class Edge
       {
       public:
          Edge();
          Edge(NodeId tail, NodeId head, Weight weight);
    
          NodeId tail() const;
          NodeId head() const;
          Weight weight() const;
    
       private:
          NodeId tail_;
          NodeId head_;
          Weight weight_;
       };
    
       Graph(NodeId nodes);
       Graph();
    
       void add_edge(NodeId tail, NodeId head, Weight costs);
       void add_edge(Edge const & edge);
       NodeId add_node();
    
       NodeId num_nodes() const;
       EdgeId num_edges() const;
       Node & node(NodeId node);
       Edge & edge(EdgeId edge);
    
       static const EdgeId invalid_edge;
       static const NodeId invalid_node;
    
    private:
       std::vector<Node> nodes;
       std::vector<Edge> edges;
    };
    
    std::ostream & operator<<(std::ostream & os, Graph::Edge const & edge);
    std::ostream & operator<<(std::ostream & os, Graph::Node const & node);
    
    #endif
    
    #include <limits>
    
    #include "graph.hpp"
    
    EdgeId const Graph::invalid_edge = std::numeric_limits<EdgeId>::max();
    NodeId const Graph::invalid_node = std::numeric_limits<NodeId>::max();
    
    std::ostream & operator<<(std::ostream & os, Graph::Edge const & edge)
    {
       os << edge.tail() << " -> " << edge.head() << " Weights: " << edge.weight();
       return os;
    }
    
    std::ostream & operator<<(std::ostream & os, Graph::Node const & node)
    {
       os << "Incoming edges: " << node.in_edges().size()
          << " Outgoing edges: " << node.out_edges().size();
       return os;
    }
    
    Graph::Graph()
    {
    }
    
    Graph::Graph(NodeId nodes)
     : nodes(nodes)
    {
    }
    
    NodeId Graph::add_node() {
       nodes.push_back(Node());
       return nodes.size()-1;
    }
    
    void Graph::add_edge(NodeId tail, NodeId head, Weight weight)
    {
       edges.push_back(Edge(tail, head, weight));
       nodes[tail].add_outgoing(edges.size()-1);
       nodes[head].add_incoming(edges.size()-1);
    }
    
    void Graph::add_edge(Edge const & edge) {
       edges.push_back(edge);
       nodes[edge.tail()].add_outgoing(edges.size()-1);
       nodes[edge.head()].add_incoming(edges.size()-1);
    }
    
    void Graph::Node::add_incoming(EdgeId edge)
    {
       incoming_.push_back(edge);
    }
    
    void Graph::Node::add_outgoing(EdgeId edge)
    {
       outgoing_.push_back(edge);
    }
    
    Graph::Edge::Edge()
     : tail_(std::numeric_limits<NodeId>::max()),
       head_(std::numeric_limits<NodeId>::max()),
       weight_(std::numeric_limits<Weight>::max())
    {
    }
    
    Graph::Edge::Edge(NodeId tail, NodeId head, Weight weight)
     : tail_(tail),
       head_(head),
       weight_(weight)
    {
    }
    
    NodeId Graph::Edge::tail() const {
       return tail_;
    }
    
    NodeId Graph::Edge::head() const {
       return head_;
    }
    
    Weight Graph::Edge::weight() const {
       return weight_;
    }
    
    std::vector<EdgeId> const & Graph::Node::in_edges() const
    {
       return incoming_;
    }
    
    std::vector<EdgeId> const & Graph::Node::out_edges() const
    {
       return outgoing_;
    }
    
    NodeId Graph::num_nodes() const
    {
       return nodes.size();
    }
    
    EdgeId Graph::num_edges() const
    {
       return edges.size();
    }
    
    Graph::Node & Graph::node(NodeId node) {
       return nodes[node];
    }
    
    Graph::Edge & Graph::edge(EdgeId edge) {
       return edges[edge];
    }
    
    include <iostream>
    #include <fstream>
    #include <sstream>
    #include <memory>
    
    #include "graph.hpp"
    #include "prng.hpp"
    #include "timer.hpp"
    #include "eba.hpp"
    
    int main(int argc, char ** argv)
    {//hier wird der graph eingelesen
    eba(*graph,mode==0);
    return 0;}
    

    Der Algorithmus soll in die Funktion eba(), deren Implementierung in eba.cpp steht. Folgende Idee habe ich gehabt.

    #include "eba.hpp"
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    Graph* findMaxWeightSubgraph(Graph& g)
    {
       Graph* b = new Graph();
       unsigned int x ;
       for (unsigned int i = 0; i < g.num_nodes(); i++)
       {
          x = b->add_node();
          //search high weight edges
          long long high = 0;
          for (unsigned int j = 0; j < g.node(x).in_edges().size();j++)
          {
             if(g.edge(g.node(x).in_edges().at(j)).weight() > high)
             {
                high = g.edge(g.node(x).in_edges().at(j)).weight();
                b->add_edge(x,g.edge(g.node(x).in_edges().at(j)).head(),high);
             }
          }
       }
    
       return b;
    }
    
    bool foundCircuit(Graph& b)
    {
       bool* visitedNodes = new bool[b.num_nodes()];
       unsigned int node;
    
       for(unsigned int i = 0;i<b.num_nodes(); i++)
       {
          visitedNodes[i] = false;
       }
    
    }
    
    void eba(Graph& input, bool mode)
    {
       unsigned int i = 0;
       vector<Graph*> g(input.num_nodes());
       g.at(i) = input;
       vector<Graph*> b(input.num_nodes());
       Graph* graphB;
       do
       {
          b.at(i) = findMaxWeightSubgraph(g.at(i)); //step 2
          //Step 4
          i++;
       }
       while(foundCircuit(b[i-1])); //step 3
       graphB = b[i-1]; //step 3
    
       while(i>0)//step 5
       {
          //step 6
       }
    
    }
    

    Und ich möchte die Graphen per Zeigern in Vektoren verwalten. Docj ich bekomme folgende Fehlermeldung.

    eba.cpp:38: Fehler: »Graph« kann nicht nach »Graph*« in assignment umgewandelt werden
    eba.cpp:43: Fehler: ungültige Initialisierung einer nicht-konstanten Referenz des Typs »Graph&« von Ausdruck des Typs »Graph*
    eba.cpp:6: Fehler: bei Übergabe des Arguments 1 von »Graph* findMaxWeightSubgraph(Graph&)«
    eba.cpp:47: Fehler: ungültige Initialisierung einer nicht-konstanten Referenz des Typs »Graph&« von Ausdruck des Typs »Graph*
    eba.cpp:28: Fehler: bei Übergabe des Arguments 1 von »bool foundCircuit(Graph&)

    Ich hoffe, dass hilft euch weiter



  • Hmm gerade sind mir die Lampe angegangen. Ich kam mit den Zeigern durcheinander.

    void eba(Graph& input, bool mode)
    {
       unsigned int i = 0;
       vector<Graph*> g(input.num_nodes());
       *g.at(i) = input;
       vector<Graph*> b(input.num_nodes());
       Graph* graphB;
       do
       {
          b.at(i) = findMaxWeightSubgraph(*g.at(i)); //step 2
          //Step 4
          i++;
       }
       while(foundCircuit(*b[i-1])); //step 3
       graphB = b[i-1]; //step 3
    
       while(i>0)//step 5
       {
          //step 6
       }
    
    }
    

    So meldet der Compiler keine Fehler mehr. Aber dennoch danke.



  • Dieser Thread wurde von Moderator/in c.rackwitz aus dem Forum ANSI C in das Forum C++ verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.


Anmelden zum Antworten