Saturday, 15 January 2011

c++ - Why is my data member empty even after construction? -


I have included the definition of both the question class and its implementation, the first is a header file and another CPP file.

I gave the comments to show that where the problem is, I can provide a question under the constraint for some reason. Only text is fine, but when I try to do it under the getQuestionText function it just produces an empty string? Any help would be most appreciated!! Thanks!

  #include & lt; String & gt; # Include & lt; Vector & gt; # Include & lt; Iostream & gt; using namespace std; #ifndef QUESTION_H # Question Define QUESTION_H class question {public: question (int thePointValue, int, theChapterNumber, \ string theQuestionText); Int getPointValue () console; Int getChapterNumber () CONST; String getQuestionText () console; Virtual Zero Writing Question (Orstream and Outfile); Virtual Word Written (Ostream and Outfile); Private: int pointValue; Int chapter number; String question text; Write zero PointValue (ostream and outfile) const; }; #endif #include "Question.h" Question :: Question (int thePointValue, int theChapterNumber, \ string theQuestionText) {pointValue = thePointValue; ChapterNumber = theChapterNumber; QuestionText = theQuestionText; // This work is perfect right here & lt; & Lt; Question & lt; & Lt; Endl; } Int Question :: getPointValue () const {Return Point Value}; } Int Question :: getChapterNumber () const {return chapterNumber; } String Question :: getQuestionText () const {// This problem is there is no lack of ITM here. Cout & lt; & Lt; Question & lt; & Lt; Endl; Return question text; } Zero Question: Written Question (Ostream and Outfile) const {writePointValue (outfile); Outfile & lt; & Lt; Question & lt; & Lt; Endl; } Zero Question: writeKey (ostream and outfile) const {writePointValue (outfile); Outfile & lt; & Lt; Endl; } Zero Question: writePointValue (ostream and outfile) const {string pt_noun; If (pointValue == 1) pt_noun = "point"; Other pt_noun = "points"; Outfile & lt; & Lt; "(" & Lt; point value <";" "& lt; pt_noun <; lt;") "; } Vector & lt; Question * & gt; Question list (string filename, int min, max max) {vector & lt; Question * & gt; QuestionList; String line; Of vector & lt; String & gt; Text; Ifstream in_file; In_file.open (filename.c_str ()); While (gateline (in_file, line)) {text.push_back (line); } String type; For (int i = 0; i & lt; text.size (); i ++) {int num = text [i] .find ('@'); Type = text [i] .substr (0, number); If (type == "multiple") {multiple questions myq = matchup (text [i]); Multiple Choice Questions * myptr = & amp; Myq; If (myq.getChapterNumber ()> gt; = min & myq.getChapterNumber () & lt; = Max) {questionList.push_back (myptr); }} If (type == "small") {shorteners questation myq = SAMatchup (text [i]); Short answer questions * myptr = & amp; Myq; If (myq.getChapterNumber ()> gt; = min & myq.getChapterNumber () & lt; = Max) {questionList.push_back (myptr); }} If (type == "long") {long and lower question myq = LAmatchup (text [i]); Long Ancer Question * myptr = & amp; Myq; If (myq.getChapterNumber ()> gt; = min & myq.getChapterNumber () & lt; = Max) {questionList.push_back (myptr); }} If (type == "code") {code question myq = CODEmatchup (text [i]); CodeQuestion * myptr = & amp; Myq; If (myq.getChapterNumber ()> gt; = min & myq.getChapterNumber () & lt; = Max) {questionList.push_back (myptr); }} Cout & lt; & Lt; Question List [Question: Song (1) - 1] - & gt; Go Q & A text () & lt; & Lt; Endl; } (Int i = 0; i & lt; question list. (); I ++) {int numm = QuestionList.size (); Cout & lt; & Lt; Question List [numm-1] - & gt; GetQuestionText () & lt; & Lt; Endl; } Return question list;   

}

Then when I call it in main code break

  vector & lt; Question * & gt; List = questionsList (pool_filname, minimum_actor, max_computer); Cout & lt; & Lt; List [0] - & gt; GetQuestionText () & lt; & Lt; Endl;    

You are announcing your code several times, the local objects and their pointer to the function At the end of the block, there will be question list vector (returned by the function) in it.

  MultipleChoiceQuestion myq = matchup (text [i]); // & lt; Local Objects Multiple Chisel Questions * myptr = & amp; Myq; // & lt; Pointer Query List for Local Objects Push_back (myptr); // & lt;   

At this point you can either use (I do not suggest you do this unless you are completely compelled, and even in that case Standard library) or store objects directly inside the vector.

No comments:

Post a Comment