gpt4 book ai didi

c++ - 如何将我的类对象插入到我的非 STL 列表中?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:36:06 25 4
gpt4 key购买 nike

我已经为这个任务工作了一段时间,并且很难插入到我的非 STL 列表中。该代码将成功编译,但每次插入我的列表时都会出现段错误。下面是一些相关的代码。

tripper.h:

class adjNode
{
public:
int vertex;
int weight;
adjNode(int v, int w) : vertex(v), weight(w) { }
adjNode() { }
};

tripper.cpp:

Tripper::Tripper(Road *roads, int numRoads, int size)
{
List <adjNode> adjList[numRoads];

for (int i=0; i<numRoads; i++) //Doesn't work with either line
{
adjList[roads[i].city1].push_back(adjNode(roads[i].city2, roads[i].distance));
//adjList[0].push_back(adjNode(2, 15)); //Really it's nothing but 3 integers involved
}
for (int i=0; i<9; i++)
{
for (List<adjNode>::iterator itr = adjList[i].begin(); itr != adjList[i].end(); itr++)
{
cout << "There is an edge going from " << i << " to " << (*itr).vertex;
cout << " with a weight of " << (*itr).weight << endl;
}
}
} // Tripper()

list.h:

void push_back( const Object & x )
{
insert( end( ), x );
}
iterator insert( iterator itr, const Object & x )
{
Node *p = itr.current;
theSize++;
return iterator( p->prev = p->prev->next = new Node( x, p->prev, p ) );
}

节点

 struct Node { 
Object data;
Node *prev;
Node *next;
Node( const Object & d = Object( ), Node * p = NULL, Node * n = NULL )
: data( d ), prev( p ), next( n )
{
}
};

最佳答案

返回迭代器( p->prev = p->prev->next = new Node( x, p->prev, p ) );

如果 p->prev 为 NULL,此行将崩溃。

关于c++ - 如何将我的类对象插入到我的非 STL 列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4175269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com