c++ - 为什么我的数据成员在构建之后还是空的?

标签 c++

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

我在其中添加了注释以显示问题所在。出于某种原因,在构造函数下我可以很好地计算问题文本,但是当我尝试在 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;

最佳答案

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

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

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

此时您可以使用 dynamic memory allocation(我建议您不要这样做,除非您绝对被迫,即使在那种情况下使用标准库提供的 smart pointers 之一)或直接将对象存储在 vector 中。

关于c++ - 为什么我的数据成员在构建之后还是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15650513/

相关文章:

c++ - 使用未分配的内存没有错误?

c++ - 在 QGraphicsWidget::paint 函数中绘制文本

c++ - 多纹理 OpenGL GLUT C++

c++ - inet_aton() 对无效 IP 地址返回成功?

c++ - 如何在 3D 中绕 Z 轴旋转

c++ - 为什么不再允许从数组的 unique_ptr 创建 shared_ptr?

c++ - while 循环外的变量在重新分配时变为 NULL

c++ - 不需要删除动态数据?

c++ - Linux -> C++ 代码 : Object Serialization

C++ 模板化生产者-消费者 BlockingQueue,无界缓冲区 : How do I end elegantly?