c++11 - Segmentation Fault on Template Function Call -
i working on introductory program singly-linked-lists , templates, using queue structure. program set have struct called "node" managed class, "queue". when run testing program (which provided professor), program hits segmentation fault upon calling "push()". i've noticed no part of either node constructor nor push() function execute -- program crashes moment push() called. other functions working perfectly. implementation node struct , push() below:
template <class t> struct node { t item; node<t>* next = nullptr; //constructor - next defaults nullptr node(const t& item) : item(item) {} //note: fails when not using init list, when nullptr not default val, etc }; template <class t> void queue<t>::push(t input) { ///////////////////////////////////////////////////////////////// /// function: push(t) /// inputs: t input: object matching type of queue /// output: none /// purpose: adds t queue @ rear position ///////////////////////////////////////////////////////////////// //!!never executes first line!! node<t> newnode(input); qback->next = &newnode; qback = &newnode; if(qsize == 0) qfront = qback; qsize++; } //note: fails when argument formatted (const t& input)... any insight appreciated. happens time push() called, char or int. if needed, queue declaration below:
template <class t> class queue { node<t>* qfront = nullptr; node<t>* qback = nullptr; size_t qsize = 0; //helper method void copylist(const queue<t>& rh); public: //constructor uses default values queue() {} //destructor (uses clear()) ~queue() { clear(); } //copy constructor queue(const queue<t>&); //operator overloads: queue<t>& operator=(const queue<t>&); friend ostream& operator<< <>(ostream&, const queue<t>&); //get methods: size_t size() const { return qsize; } bool empty() const; t& front() const; t& back() const; //status modifiers: void clear(); //content modifiers: void push(t); void pop(); }; the code calls push() looks this:
queue<int> q1; //some cout statements testing empty() , size()... q1.push(17); //fails here //more code can run if comment out push()'s //all pushes fail, regardless of value or how value passed //e.g. using "int n = 17;" , passing n still fails if additional information needed, gladly supply it. cannot figure out why function fails... record: using g++ compiler on linux server students ssh into.
Comments
Post a Comment