Vector losing contents between files in C++ -


i have methods in lexer.h make use of vector made of tokens.

in method void getnexttoken() making use of said vector adding new tokens it.

the problem is, when go different file, trying access method makes use of vector, crashing out of bounds error (most it's being deferenced or something)

is there way how can fix this?

the methods in concern are:

    token* nexttoken()     {         if (it!= tokensused.end())         {             // assigned found in iterator (of vector)             // data found in pointer             itrtoken = &*it;             //move iterator forward             ++;             return itrtoken;         }     }      /*         used in parser go previous tokens     */     token* prevtoken()     {         itrtoken --;          if (it!= tokensused.begin())         {             itrtoken = &*this->it;             return itrtoken;         }     }  void getnexttoken() {   //code  adding tokens //example if (ch == '"')         {             addandgetnext(ch);             cout << "string:  " << strbuffer << endl; //test             //create new token , push vector              tk = new token (token::ttstring, strbuffer, row, col);             tokensused.push_back(*tk); //add new token vector             startnewstring(); //clear string         }         tokenmatch = true; } 

the above partial code, show example.

now in parser.h using method call lexer.h:

   void relopp()     {         token* tk = nexttoken();         if (tk -> gettype() == token::ttstring)         {             cout << "ture";         }     } 

which calls lexer's nexttoken() crashes, , when tried checking it's contents goes outofbounds error (and codeblocks giving me sigsegv error)

i know it's pointers it's going awry, how can fix it?

edit:

these global variables have declared:

vector<token>::iterator it; vector<token> tokensused;   token* itrtoken; // used iterator   bool checkquote = false;  token* tk = new token (synewtoken, "", 1,0);   token token; // creates instance of class token found in file token.h                 token* t; 

sample code:

main.cpp

#include <iostream>  #include "lexer.h" #include "parser.h" using namespace std;  int main() {     lexer* l;      l -> getnexttoken();      parser p(l);     p.relopp(); }       token (int type, string sbuffer, int rrow, int ccol)     {         this->ttype = type;         this->strbuffer = sbuffer;         this->row = rrow;         this->col = ccol; } 

parser.h

#ifndef parser_h_included #define parser_h_included   #include <string> #include <vector> #include "lexer.h" #include "token.h"  using namespace std;  class parser{ private:      lexer* lexer;     string tree = "";  public:      parser (lexer* l)     {         -> lexer = l;     }      token nexttoken()     {         token tk = lexer -> nexttoken();         return tk;     }        void relopp()     {         token tk = nexttoken();         if (tk.gettype() == 1)         {             cout << "ture";         }     }    #endif // parser_h_included }; 

token.h

#ifndef token_h_included #define token_h_included   #include <iostream>  using namespace std;   class token { private:      int ttype; //identifier or reserved compiler?     string strbuffer; //string found in buffer @ moment     int row;     int col;  public:       enum tokentype     {         tkstring     };      token()     {      }      // instance of token 4 parameters resulting type, contents of string represents type     // row found in , column.     token (int type, string sbuffer, int rrow, int ccol)     {         this->ttype = type;         this->strbuffer = sbuffer;         this->row = rrow;         this->col = ccol;     }         token (token* gett)     {         this-> ttype = gett -> ttype;         this->strbuffer = gett -> strbuffer;         this->row = gett -> row;         this->col = gett -> col;     }       int gettype ()     {         return this->ttype;     }      //return string contents     string getbuffer()     {         return this->strbuffer;     }      //return row     int getrow()     {         return row;     }      //return col     int getcol ()     {         return col;     } }; #endif // token_h_included 

lexer.h

    #ifndef lexer_h_included     #define lexer_h_included      #include "token.h"     #include <vector>     using namespace std;      class lexer     {      private:           token tk = new token (1, "", 1,0);          vector<token>::iterator it;          vector<token> tokensused;         token itrtoken; // used iterator      public:            token nexttoken()             {                 if (it!= tokensused.end())                 {                     // assigned found in iterator (of vector)                     // data found in pointer                     itrtoken = &*it;                     //move iterator forward                     ++;                     return &itrtoken;                 }                 else                 {                     cout << "error" << endl;                  }                 return nullptr;              }               void getnexttoken()              {                  cout << "test" << endl;                  string strbuffer = "test";                  int row = 0;                  int col = 0;                  tk = new token (1,strbuffer,row,col);              }       };       #endif // lexer_h_included 

in nexttoken() , prevtoken() there no return case if evaluates false. return value in case not (it anything...) can dereference.

if want keep current design should return nullptr or (null if don't have c++11 support) in case. need change code uses result of functions check if pointer valid before dereferencing it.

you better changing design not involve manual pointer manipulation. fix current version should change prevtoken , nexttoken along lines of:

token* nexttoken() {     if (it!= tokensused.end())     {        ...         return itrtoken;     }     else     {        return nullptr;     } } 

then if tk result of calling 1 of these functions must not use tk-> or *tk if nullptr. code wanting work result need check first.

so example change if statement be:

if (tk && // make sure tk not nullptr     tk -> gettype() == token::ttstring) {     ... 

Comments

Popular posts from this blog

c++ - OpenCV Error: Assertion failed <scn == 3 ::scn == 4> in unknown function, -

php - render data via PDO::FETCH_FUNC vs loop -

The canvas has been tainted by cross-origin data in chrome only -