c++ - Not getting correct address to head and tail nodes in copy constructor doubly linked list -
copy constructor:
// re-create constructor. copies list. crevlist(const crevlist &b) { cout << "copy" << endl; const node *start = b.begin(); const node *end = b.end(); cout << "start; " << start << endl; cout << "end; " << end << endl; cout << "b.begin() : " << b.begin() << endl; cout << "b.end() : " << b.end() << endl; t temp_data; for(;;){ cout << "start->data() loop: " << start->data() << endl; temp_data = start->data(); pushback(temp_data); if(start == end) break; start = start->m_next; }
}
call end pointer tail node (begin() same):
const node *end() const {} node *end() { cout << "m_tail " << m_tail << endl; homecoming m_tail; }
sorry amount of code. can't figure out i'm going wrong
edit: okay here's minimal finish example
driver
using namespace std; #include <iostream> #include "revlist.h" int main(){ crevlist<int> list; list.pushback(7); list.pushback(300); cout << "end ref: " << list.end() << endl; cout << "begin ref: " << list.begin() << endl; cout << list.end() << endl; crevlist<int> new_list(list); cout << "end ref: " << new_list.end() << endl; cout << "begin ref: " << new_list.begin() << endl; }
implementation of doubly linked list:
template<class t> class crevlist { public: //constructor stuff doesn't matter... ; class node { public: friend class crevlist; node() {m_next = 0; m_prev = 0;} node(const t &t) {m_payload = t; m_next = 0; m_prev = 0;} t data() {return m_payload;} const t data() const {return m_payload;} private: node *m_next; node *m_prev; t m_payload; }; //push //////////////////////////////////////////////// void pushback(const t &t) { node * temp = new node(t); if(isempty()){ cout << "is empty" << endl; m_head = temp; m_tail = temp; } else{ node * curr = m_tail; curr->m_next = temp; temp->m_prev = curr; m_tail = temp; } size += 1; } //get pointer first node in list const node *begin() const {} node *begin() { cout << "m_head " << m_head << endl; homecoming m_head; } //get pointer lastly node in list const node *end() const {} node *end() { cout << "m_tail " << m_tail << endl; homecoming m_tail; } private: node *m_head, *m_tail; // head node unsigned size; }; };
and output driver
m_tail 0x2068030 end ref: 0x2068030 m_head 0x2068010 begin ref: 0x2068010 m_tail 0x2068030 0x2068030 re-create start; 0x7fff7745d240 end; 0x7fff7745d240 b.begin() : 0x7fff7745d240 b.end() : 0x7fff7745d240 start->data() loop: 2 segmentation fault //don't care right
you forgot initialize size
.
c++ function pointers reference
No comments:
Post a Comment