0
/* Copy constructor */
List(const List<value_type>& list)
{
// for (iterator it = list.begin(); it != list.end(); ++it);
// this->push_back(*it);
// The commented part above is what I want it to do.
std::cout << "empty = " << this->empty(); // no seg fault;
std::cout << "size = " << this->size(); // also causes a seg fault
this->push_back("string") // causes a seg fault
}
이 코드를 실행하면 프로그램에 seg fault가 발생합니다. 변경하거나 변경하려고 할 때마다 (단지) 단지 오류가 발생합니다.왜 복사 생성자가 개체를 변경하려고 할 때마다 segfault를 생성합니까?
은 또한 그것은
는 여기에 추가 정보를 원하시면라는 방법에 대한 코드 (즉 독방 감금 오류가 발생하지 않는 유일한 하나가 될 것으로 보인다) (이) 비어 있지 말한다. 누군가 여기에서 일어나고있는 일에 대해 어떤 통찰력을 줄 수 있기를 바랍니다.
void insert(iterator position, const value_type& in)
{
// If inserting at the front, just change the head to a new Node
if (position == this->head)
this->head = new Node<value_type>(in);
else
{
Node<value_type>* node = this->head;
// iterate to the position of "position".
for (; node->next != position.node; node = node->next);
node->next = new Node<value_type>(in);
}
// This is here to put back all the old data into it's correct position.
// I was having way too much with keeping the integrity of the data
// while inserting into the middle of the list, so I created this little hack.
for (iterator it = position; it != this->end(); ++it)
{
Node<value_type> *node = this->head;
for (; node->next != NULL; node = node->next);
node->next = new Node<value_type>(it.node->data);
}
}
// Insert at end
void push_back(value_type in)
{
this->insert(this->end(), in);
}
unsigned size()
{
if (this->empty()) return 0;
unsigned i = 0;
for (iterator it = this->begin(); it != this->end(); ++it, ++i);
return i;
}
bool empty() const { return this->head == NULL; }