C++ Strange behavior in my own stack class -
here program stack class , functions.
readthefile()
- reads numbers, stored innum_file.txt
, , returns vector numbers.intervalcheck()
- adds numbers of specific range input vector , returns vector numbers only.vectomystack()
- adds numbers vector stack.#include <iostream> #include <string> #include <vector> #include <fstream> #define stack_empty -1 #define out_of_stack -2 using namespace std; template <class t> class stack { private: struct node{ t element; node *prevelement; }; size_t numberofelements; node *tempadr; node *topelement; node *newelement; node *erasedelement; public: stack(){ topelement = new node; topelement->prevelement = nullptr; numberofelements = 0; } ~stack(){ cout << endl << "i'm destructor"; while(numberofelements !=0 ){ tempadr = topelement->prevelement; delete topelement; topelement = tempadr; numberofelements--; } delete topelement; } void push(t input_element){ tempadr = topelement; topelement = new node; topelement->element = input_element; topelement->prevelement = tempadr; numberofelements++; } void pop(){ if (numberofelements == 0) throw stack_empty; else { tempadr = topelement->prevelement; delete topelement; topelement = tempadr; numberofelements--; } } t top(){ return numberofelements != 0 ? topelement->element : throw stack_empty; } void insert(size_t position, t input_element){ if (position >= numberofelements) throw out_of_stack; else { tempadr = topelement; (size_t = 0; < position; i++){ tempadr = tempadr->prevelement; } newelement = new node; newelement->element = input_element; newelement->prevelement = tempadr->prevelement; tempadr->prevelement = newelement; numberofelements++; } } void erase(size_t position){ if (position >= (numberofelements-1)) throw out_of_stack; else{ tempadr = topelement; (size_t = 0; < position; i++){ tempadr = tempadr->prevelement; } erasedelement = tempadr->prevelement; tempadr->prevelement = tempadr->prevelement->prevelement; delete erasedelement; numberofelements--; } } void print(){ if (numberofelements != 0){ tempadr = topelement; (size_t = 0; < numberofelements; i++){ cout << tempadr->element << " "; tempadr = tempadr->prevelement; } } } size_t size() { return numberofelements; } }; vector<int> readthefile() { vector<int> vec_from_file; int buffer; ifstream basefile; basefile.open("num_file.txt", ios::in); if (basefile.is_open()) { { if (basefile >> buffer) vec_from_file.push_back(buffer); else { basefile.clear(); basefile.ignore(1, ' '); } } while (!basefile.eof()); basefile.close(); } else cout << "unable open file" << endl; return vec_from_file; } vector<int> intervalcheck(vector<int> vec_for_check){ vector<int> out_vec; if (vec_for_check.empty()) cout << "there nothing check"; else { int begin_int, end_int; { cin.clear(); cin.sync(); cout << "input first , last value of interval: "; cin >> begin_int >> end_int; } while (cin.fail()); (auto &k : vec_for_check) if (k > begin_int && k < end_int) out_vec.push_back(k); } return out_vec; } stack<int> vectomystack(vector<int> input_vec){ stack<int> output_st; if (input_vec.empty()) { cout << "the end"; } else { (auto &k : input_vec){ output_st.push(k); } } return output_st; } int main(){ int choice = 0; { cin.clear(); cin.sync(); vectomystack(intervalcheck(readthefile())).print(); cout << "would measure interval? 1-yes 2-no"; cin >> choice; } while (choice == 1); system("pause"); return 0; }
the whole program should push numbers file stack, , print stack, using print()
method of class. example, if there num_file.txt
0 1 2 3 4 5 6 7 8 9 10
inside, program expected work in way:
- input first , last value of interval: 0 10 /* 0 , ten inputed user*/
- 1 2 3 4 5 6 7 8 9
- would measure interval? 1-yes 2-no
but when vectomystack(intervalcheck(readthefile())).print();
line executed, i'm getting
access violation reading location 0xfeeefeee.
exception. seemes destructor of stack class running before print()
function. why happen? there special should add stack class or vectomystack()
function?
finally, after couple of hours of research, i've got missing peace of code:
stack(const stack &object){ tempadr = object.topelement; t * tempmas=new t[object.numberofelements]; (size_t = 0; < object.numberofelements; i++){ tempmas[i] = tempadr->element; tempadr = tempadr->prevelement; } topelement = new node; topelement->prevelement = nullptr; numberofelements = 0; (int = object.numberofelements - 1; >= 0; i--){ push(tempmas[i]); } delete[] tempmas; }
i know stack class still uncomplete without overloaded assignment operator, @ least code runs fine.
Comments
Post a Comment