在我的c ++程序中找不到错误

时间:2015-11-28 13:25:52

标签: c++ c++11

这是实现,代码是用于prim的算法,但目前还不完整。

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_ */

gcc(windows,minGW)显示的错误是: C:\ Users \ cortana \ AppData \ Local \ Temp \ ccpIsRfk.o:Controller.cpp :(。text + 0xaa):对`prim :: prim(Graph *)'的未定义引用 collect2.exe:错误:ld返回1退出状态

编辑:

我做了一切,但仍然收到错误:

我得到我的输出,垃圾堆,然后是一些windows url(输出后的dll和东西的奇怪列表)然后是一个未处理的异常,说a.exe停止工作。

Visual Studio调试器说:     
a.exe中0x7559DC60(msvcrt.dll)的未处理异常:0xC0000005:访问冲突读取位置0x002A4000。

1 个答案:

答案 0 :(得分:1)

您尝试将Controller.cpp编译为不带prim.cpp的可执行文件。要么这样做:

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

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

要解决vtable错误,请参阅the second answer here:您忘记实施prim::~prim