非常感谢您的评论,我是新来的,不熟悉协议。这是我的完整例子。这就是我执行的内容
在顶点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;
}
答案 0 :(得分:2)
vertex* v = new vertex("vertex nbr "+i);
在C ++中,您无法在operator+
和const char*
上使用int
。必须以其他方式构造一个字符串。我很惊讶编译器没有写出关于它的警告,更别说停在那里了。
非常重要:vertex
构造函数和析构函数是什么样的?
这篇文章应该提供答案,但我会给你更多问题。那是因为你自己的问题缺乏相关信息。你给了一些代码,这很好,但我无法编译该代码来重现错误。努力将示例代码减少到最低限度,以重现错误。这可能需要很长时间,你很可能自己找到问题所在。那将是完美的结果。