samedi 28 novembre 2015

Can't find error in my c++ program

Here's the implementation, code is meant for prim's algo but is incomplete at the moment.

prim.h

#include "graph.h"
#include "header.h"

class prim {
private:
    vector <string> list;
public:
    prim ();
    prim (Graph *g);
    virtual ~prim ();
};

prim.cpp

#include "prim.h"
#define infinity std::numeric_limits <int>::infinity ()

prim::prim () {
}

prim::prim (Graph *g) {
    g -> printGraph ();
}

graph.h

#include "header.h"
#include <string>
using namespace std;

class vertex;
class edge {
private:
    vertex * origin;
    vertex * destination;
    int weight;
public:
    edge(vertex * org, vertex * dest, int weight) {
        origin = org;
        destination = dest;
        this->weight = weight;
    }

    vertex * getOrigin() {
        return origin;
    }
    vertex * getDestination() {
        return destination;
    }
    int getWeight() {
        return weight;
    }
};

class vertex {
private:
    string name;
    vector<edge> edges;
    int cost_of_each_node;
    bool source;
public:
    vertex(string id) {
        name = id;
        cost_of_each_node = NULL;
        source = false;
    }

    vertex(string id, int cost) {
        name = id;
        cost_of_each_node = cost;
        source = false;
    }

    vertex(string id, int cost, bool source) {
        name = id;
        cost_of_each_node = cost;
        this -> source = source;
    }

    void update_cost (int new_cost) {

    }
    void addEdge(vertex * v, int dist) {
        edge newEdge(this, v, dist);
        edges.push_back(newEdge);
    }

    void printEdges() {
        cout << name << " : " << endl;
        for (int i = 0; i < edges.size(); ++i) {
            edge e = edges[i];
            cout << e.getDestination()->getName() << " - " << e.getWeight()
                    << endl;
        }
        cout << endl;
    }

    string getName() {
        return name;
    }
    vector<edge> getEdges() {
        return edges;
    }
    int getCost() {
        return cost_of_each_node;
    }
    bool if_source() {
        return source;
    }
};
class Graph {
private:
    //vector <vertex *> vertices;
    string name;
public:
    vector<vertex *> vertices;
    //vector <string>
    Graph() {
    }

    Graph(string name) {
        this->name = name;
    }

    void insert(vertex * v) {
        vertices.push_back(v);
    }

    void printGraph() {
        for (int i = 0; i < vertices.size(); ++i) {
            vertices[i]->printEdges();
        }
    }
    int return_size() {
        return vertices.size();
    }

    string getName() {
        return name;
    }
};
class data_for_graph {
public :
    data_for_graph (Graph * g) {
        vertex v1 = vertex("Seattle");
        vertex v2 = vertex("Portland");
        vertex v3 = vertex("Everett");
        vertex v4 = vertex("Lynnwood");
        vertex v5 = vertex("Northgate");
        vertex v6 = vertex("Bellevue");
        vertex v7 = vertex("Arlington");
        vertex v8 = vertex("Bellingham");


        vertex *vp1 = &v1;
        vertex *vp2 = &v2;
        vertex *vp3 = &v3;
        vertex *vp4 = &v4;
        vertex *vp5 = &v5;
        vertex *vp6 = &v6;
        vertex *vp7 = &v7;
        vertex *vp8 = &v8;


        v1.addEdge(vp2, 100);
        v1.addEdge(vp6, 20);
        v2.addEdge(vp1, 100);
        v3.addEdge(vp1, 30);
        v3.addEdge(vp4, 10);
        v3.addEdge(vp7, 20);
        v4.addEdge(vp5, 15);
        v5.addEdge(vp1, 10);
        v6.addEdge(vp1, 20);
        v8.addEdge(vp7, 45);


        g -> insert(vp1);
        g -> insert(vp2);
        g -> insert(vp3);
        g -> insert(vp4);
        g -> insert(vp5);
        g -> insert(vp6);
        g -> insert(vp7);
        g -> insert(vp8);


        g -> printGraph();
    }
};

#endif /* SRC_GRAPH_H_ */

the error shown by gcc (windows, minGW) is : C:\Users\cortana\AppData\Local\Temp\ccpIsRfk.o:Controller.cpp:(.text+0xaa): undefined reference to `prim::prim(Graph*)' collect2.exe: error: ld returned 1 exit status

Aucun commentaire:

Enregistrer un commentaire