c++ - Output to file giving wrong pointer values -


i have class word holds string , vector of structs, each struct containing pointer container class, so:

struct doc {     document* d;     int timesmentioned = 0; }; 

i have output function outputs each word's string , every doc's key in word's doc vector. (d1, d2, d3, etc..)

whenever output of them file, looks this:

foo = d3, bar = d3, foobar = d3, etc. 

with every word outputting d3 (the last document looped through previous given input file. can post code if needed)

the weird thing is, whenever output console, works fine. doc that's associated each word outputted correctly. idea why happen? thought may dangling pointer outputting console correctly curious. can post adding docs function or more code if needed.

here code word class:

void word::createword(string str) {     removepunctuation(str);     tolower(str);     word = str; } void word::adddoc(document& d) {     doc newdoc;     newdoc.d = &d;     newdoc.timesmentioned++;     docs.push_back(newdoc); } vector<doc> word::getdocs() {     return docs; } string word::getword() {     return word; }  //formatting string, should irrelevant question void word::removepunctuation(string& str) {     string temp = "";     for(int = 0; < str.length(); i++) {         bool punctual = false;         for(int k = 0; k < 42; k++)             if(str[i] == punctuals[k]) punctual = true;         if(!punctual) temp += str[i];     }     str = temp; } void word::tolower(string& str) {     string temp = "";     for(int = 0; < str.size(); i++) {         char c = str[i];         temp += tolower(c);     }     str = temp; } 

here's words/documents instantiated , added vector:

void invertedfileindex::parsefile(string filename) {     fstream fin, fout;     fin.open(filename.c_str(), fstream::in);      if(fin.is_open()) {         //parse input single string xmlparsing         xml_document<> doc;         string str, parse = "";         while(fin >> str)                 parse += str + " ";         doc.parse<0>(&parse[0]);         xml_node<>* root;         root = doc.first_node("mediawiki");          //iterate through each page         int = 1;         (xml_node<>* page = root->first_node("page"); page; page = page->next_sibling()) {             document d;             word w;             string docname = "d" + inttostr(i);             xml_node<>* title = page->first_node("title");             d.settitle(page->first_node("title")->value());             d.setkey(docname);              //find text document             xml_node<>* revision = page->first_node("revision");             xml_node<>* text = revision->first_node("text");             d.settext(text->value());             string doctext = text->value();              ////begin parsing text words             stringstream ss(doctext); //create string stream can break tokens             string item;             while (getline(ss, item, ' ')) {                 word w;                 w.createword(item);                 w.adddoc(d);                 toks.push_back(w);             }             documents.push_back(d);             writetoindex();             i++;         }         fin.close();     }     else         cout << "the sample file " << filename << "could not opened." << endl; } 

and here's output code:

void invertedfileindex::writetoindex() {     fstream fout;     fout.open("index.txt", fstream::out);      if(fout.is_open()) {         for(int = 0; < toks.size(); i++) {             fout << toks[i].getword() << " = ";             cout << toks[i].getword() << " = ";             for(int j = 0; j < toks[i].getdocs().size(); j++) {                 fout << toks[i].getdocs()[j].d->getkey();                 cout << toks[i].getdocs()[j].d->getkey();                 fout << ", ";                 cout << ", ";             }             fout << endl;             cout << endl;         }         fout.close();     }     else         cout << "index file not opened." << endl; } 

look's me you're saving pointers objects go out of scope. code is

while (...) {     document d;     ...     w.add(d); // add takes reference , stores pointer d }  // sometime later writetoindex(); 

obviously time writetoindex document objects, created in while loop, have been destroyed. have pointers objects no longer exist. if want store pointers, should dynamically allocate objects somewhere.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -