-3

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

EDIT:

I did everything and still am getting an error:

I am getting my output , loads of garbage, then some windows urls (weird list of dlls and stuff after the output) and then an unhandled exception saying a.exe stopped working.

Visual studio debugger says :
Unhandled exception at 0x7559DC60 (msvcrt.dll) in a.exe: 0xC0000005: Access violation reading location 0x002A4000.

kneelb4darth
  • 305
  • 3
  • 14

1 Answers1

1

You're attempting to compile Controller.cpp into an executable without prim.cpp. Either do this:

g++ Controller.cpp prim.cpp -o executable.exe

or

g++ -c Controller.cpp
g++ -c prim.cpp
g++ Controller.obj prim.obj -o executable.exe

To fix the vtable error, see the second answer here: you forgot to implement prim::~prim.

Community
  • 1
  • 1
Kenney
  • 9,003
  • 15
  • 21
  • thanks a lot for the help @Kenney – kneelb4darth Nov 28 '15 at 13:34
  • hey now its giving the error : C:\Users\cortana\AppData\Local\Temp\ccsTeH5B.o:prim.cpp:(.text+0xe): undefined reference to `vtable for prim' C:\Users\cortana\AppData\Local\Temp\ccsTeH5B.o:prim.cpp:(.text+0x31): undefined reference to `vtable for prim' collect2.exe: error: ld returned 1 exit status – kneelb4darth Nov 28 '15 at 13:35
  • Sorry, I meant `g++` instead of `gcc` - it will do some C++ magic. Do you only have the 2 cpp files, or are there more? – Kenney Nov 28 '15 at 13:38
  • thats it and I did the g++ part by myself. The output is after compiling it with g++ – kneelb4darth Nov 28 '15 at 13:39
  • When your project is in it's own folder you could always just compile with `g++ *.cpp -o executable.exe`. – Hatted Rooster Nov 28 '15 at 13:40
  • If it isn't, is there a way to string it all together instead of writing names of files like that ?? – kneelb4darth Nov 28 '15 at 13:42
  • You could look into makefiles. But maybe you just need to put everything in its own folder and focus on the basics first? – CompuChip Nov 28 '15 at 13:47
  • Updated the answer - the vtable error has many causes, but in your case you are missing the implementation of the `prim` destructor. – Kenney Nov 28 '15 at 13:47
  • @CompuChip I aint dumb bro everything in the folder, it really is – kneelb4darth Nov 28 '15 at 13:52
  • @Kenney thanks again – kneelb4darth Nov 28 '15 at 13:53
  • I am getting my output , loads of garbage, then some windows urls and then an unhandled exception saying a.exe stopped working.Visual studio debugger says : Unhandled exception at 0x7559DC60 (msvcrt.dll) in a.exe: 0xC0000005: Access violation reading location 0x002A4000. – kneelb4darth Nov 28 '15 at 14:10