基本指针/参考问题

时间:2013-11-02 13:39:59

标签: c++

非常感谢您的评论,我是新来的,不熟悉协议。这是我的完整例子。这就是我执行的内容

在顶点ctor 加载顶点[0] = xxx 在顶点ctor 加载顶点[1] = xxx 在顶点ctor 加载顶点[2] = xxx 在顶点ctor 加载顶点[3] = xxx 在顶点ctor 加载顶点[4] = xxx 在顶点ctor 加载顶点[5] = xxx 在顶点ctor 加载顶点[0] = xxx calculateCosts 得到顶点名为xxx 移动顶点名为xxx DONE 分段错误

    #include <iostream> 
#include <vector>   
#include <cassert>  
#include <limits>
#include <initializer_list>
#include <iterator>

// required compile option g++ homeWork2.cpp -std=c++0x

using namespace std;

const int infinity = numeric_limits<int>::infinity();
//const   vertex* nullPtr = 0;

class vertex
{
public:
    string name;
    vertex(string name = "undefined")
    {
        cout << "in vertex ctor " << endl;
        name = "AAAAAA";
        value = infinity;
        prev_vertex = NULL;
    }
    void set_value(int& value)
    {
        assert(value > 0);
        value = value;
    }
    int get_value()
    {
        return value;
    }
    void set_prev_vertex(vertex& prev_vertex)
    {
        prev_vertex = prev_vertex;
    }
    vertex* get_prev_vertex()
    {
        return prev_vertex;
    }

private:
    int value;
    vertex * prev_vertex;
};


class graph
{
public:
    graph(int graphSize = 50)
    {
        for (int i = 0; i < graphSize; ++i)
        {
            vertex* pV = new vertex("vertex name");
            pV->name = "xxx";
            cout << "loading vertex[ " << i << "] = " << pV->name << endl;
            vertices.push_back(pV);
        }
    }

//    vector<vertex> get_neighbors(int i);

    void push_Vertex(unsigned position, vertex* v);
    void remove_Vertex(unsigned position);
    vertex* get_Vertex(unsigned position);
    int size();
    vector<vertex*> get_neighbors(int i);

    ~graph()
    {
        while (vertices.size() > 0)
        {
//          cout << "removing vertices[ " << vertices.size() << "] = " << vertices.back() << endl;
            delete vertices.back();
            vertices.pop_back();
        }
    }

private:
    int graph_count;
    vector<vertex*> vertices;
};

void graph::push_Vertex(unsigned position, vertex* v)
{
//      cout << "copying vertex named " << *v->name << endl;
//        assert(v!=NULL);
        vertices[position] = v;
//      vertices.push_back(v);
}
void graph::remove_Vertex(unsigned position)
{
    vertices.erase(vertices.begin()+position);
}
vertex* graph::get_Vertex(unsigned position)
{
//  vector<int>::iterator iter = vertices.begin()+=position;
    vertex* pV = vertices.at(position);
    cout << "got vertex named " << pV->name << endl;
    return pV;
}

vector<vertex*> graph::get_neighbors(int i)
{
    vector<vertex*> vertices;
    /* load the it neighbors*/
    return vertices;
}

int graph::size()
{
    return vertices.size();
}

void moveVertex(graph& fromSet, graph& toSet, int position, int value)
{
    vertex* v = fromSet.get_Vertex(position);
    cout << "moving vertex named " << v->name << endl;
    toSet.push_Vertex(0, v);

}

void calculateCosts(graph& unvisitedSet, graph& visitedSet)
{
    moveVertex(unvisitedSet, visitedSet, 0, 0);
    for (int i =0; i < unvisitedSet.size(); ++i) {
//  do the algo process ...        
    }
}

// 
int main()
{

//    adjacency_list adjList = adjacency_list(6);
//    cost_list costList = cost_list(6);
    graph visitedSet = graph(6);
    graph unvisitedSet = graph(1);
    cout << "calculateCosts" << endl;
    calculateCosts(visitedSet, unvisitedSet);

    cout << "done" << endl;

    return 0;
}

1 个答案:

答案 0 :(得分:2)

        vertex* v = new vertex("vertex nbr "+i);

在C ++中,您无法在operator+const char*上使用int。必须以其他方式构造一个字符串。我很惊讶编译器没有写出关于它的警告,更别说停在那里了。

编辑:我现在看到行“calculateCosts移动顶点名为完成分段错误”。它非常不起眼,我第一次没有注意到它,但现在我看到它是程序的输出。似乎导致分段错误的代码在其中一个析构函数中。调试器是否具有在发生段错误时暂停执行的功能?你能上交吗?您可以逐步调试代码,包括析构函数中的代码吗?如果逐步调试会发生什么?

非常重要:vertex构造函数和析构函数是什么样的?

这篇文章应该提供答案,但我会给你更多问题。那是因为你自己的问题缺乏相关信息。你给了一些代码,这很好,但我无法编译该代码来重现错误。努力将示例代码减少到最低限度,以重现错误。这可能需要很长时间,你很可能自己找到问题所在。那将是完美的结果。