gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:42:00 25 4
gpt4 key购买 nike

我已经包含了我对 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com