0

我已经包含了 Question 类的定义及其实现,第一个是头文件,第二个是 cpp 文件。

我发表评论以显示问题所在。出于某种原因,在构造函数下,我可以很好地计算 questionText,但是当我尝试在 getQuestionText 函数下执行此操作时,它只会输出一个空字符串?非常感激任何的帮助!!谢谢!

#include <string>
#include <vector>
#include <iostream>

using namespace std;

#ifndef QUESTION_H
#define QUESTION_H

class Question{
public:
    Question(int thePointValue, int theChapterNumber, \
        string theQuestionText);
    int getPointValue() const;
    int getChapterNumber() const;
    string getQuestionText() const;
    virtual void writeQuestion(ostream& outfile) const;
    virtual void writeKey(ostream& outfile) const;

private:
    int pointValue;
    int chapterNumber;
    string questionText;

    void writePointValue(ostream& outfile) const;
};




#endif





#include "Question.h"

Question::Question(int thePointValue, int theChapterNumber, \
        string theQuestionText)
{
    pointValue = thePointValue;
    chapterNumber = theChapterNumber;
    questionText = theQuestionText;

        //HERE THIS WORKS PERFECTLY
        cout << questionText << endl;
}

int Question::getPointValue() const
{
    return pointValue;
}

int Question::getChapterNumber() const
{
    return chapterNumber;
}

string Question::getQuestionText() const
{
        //THIS IS THE PROBLEM. HERE IT OUPUTS AN EMPTY STRING NO MATTER WHAT!
        cout << questionText << endl;
    return questionText;
}

void Question::writeQuestion(ostream& outfile) const
{
    writePointValue(outfile);
    outfile << questionText << endl;
}

void Question::writeKey(ostream& outfile) const
{
    writePointValue(outfile);
    outfile << endl;
}

void Question::writePointValue(ostream& outfile) const
{
    string pt_noun;

    if (pointValue == 1)
        pt_noun = "point";
    else
        pt_noun = "points";

    outfile << "(" << pointValue << " " << pt_noun << ") ";
}

vector<Question *> QuestionsList(string filename, int min, int max)
{
vector<Question *> QuestionList;

string line;
vector<string> text;
ifstream in_file;
in_file.open(filename.c_str());
while (getline(in_file, line))
{
    text.push_back(line);
}

string type;
for(int i = 0; i < text.size(); i ++)
{
    int num = text[i].find('@');
    type = text[i].substr(0, num);
    if (type == "multiple")
    {
        MultipleChoiceQuestion myq = matchup(text[i]);
        MultipleChoiceQuestion* myptr = &myq;
        if (myq.getChapterNumber() >= min && myq.getChapterNumber() <= max)
        {
            QuestionList.push_back(myptr);
        }
    }
    if (type == "short")
    {
        ShortAnswerQuestion myq = SAmatchup(text[i]);
        ShortAnswerQuestion* myptr = &myq;
        if (myq.getChapterNumber() >= min && myq.getChapterNumber() <= max)
        {
            QuestionList.push_back(myptr);
        }
    }
    if (type == "long")
    {
        LongAnswerQuestion myq = LAmatchup(text[i]);
        LongAnswerQuestion* myptr = &myq;
        if (myq.getChapterNumber() >= min && myq.getChapterNumber() <= max)
        {
            QuestionList.push_back(myptr);
        }
    }
    if (type == "code")
    {
        CodeQuestion myq = CODEmatchup(text[i]);
        CodeQuestion* myptr = &myq;
        if (myq.getChapterNumber() >= min && myq.getChapterNumber() <= max)
        {
            QuestionList.push_back(myptr);
        }
    }
    cout << QuestionList[QuestionList.size()-1]->getQuestionText() << endl;
}
for (int i = 0; i < QuestionList.size(); i ++)
{
    int numm = QuestionList.size();
    cout << QuestionList[numm-1]->getQuestionText() << endl;
}
return QuestionList;

}

然后当我在 main 中调用它时,代码会中断

vector<Question *> list = QuestionsList(pool_filename, min_chapter, max_chapter);
cout << list[0]->getQuestionText() << endl;
4

1 回答 1

1

您在代码中多次声明本地对象并将它们的指针存储到QuestionList向量(由函数返回)中,该向量在函数块的末尾将包含悬空指针

MultipleChoiceQuestion myq = matchup(text[i]); // < local object
MultipleChoiceQuestion* myptr = &myq; // < pointer to local object

QuestionList.push_back(myptr); // < push back into vector

此时,您可以使用动态内存分配(我建议您不要这样做,除非您绝对强制,即使在这种情况下使用标准库提供的智能指针之一)或将对象直接存储在向量中。

于 2013-03-27T02:38:42.017 回答