使用:MSVS2012
代码
elemalg.h
#include <vector>
#include <string>
#include <fstream>
class ElemAlg
{
private:
std::string difficultlyLevel, question, answerToRead;
std::vector<std::string> questions, answers;
std::vector<std::string> GetQuiz(int);
};
elemalg.cpp
#include "elemalg.h"
std::vector<std::string> ElemAlg::GetQuiz(int difficulty)
{
if (difficulty == 1) { difficultyLevel = "algE"; }
if (difficulty == 2) { difficultyLevel = "algM"; }
if (difficulty == 3) { difficultyLevel = "algH"; }
if (difficulty == 4) { difficultyLevel = "algVH"; }
std::ifstream fin(difficultyLevel + ".txt");
while (std::getline(fin, question)) { questions.push_back(question); }
fin.close();
std::ifstream fin2(difficultyLevel + "Answers.txt");
while (std::getline(fin2, answerToRead)) { answers.push_back(answerToRead); }
fin2.close();
return questions;
}
MathTutor.cpp
#includes etc
ElemAlg *ea;
ea->GetQuiz(1);
GetQuiz
肯定会传递一个介于1和4之间的整数,在调用该方法之前进行验证。
difficultyLevel
是在头文件中定义的字符串。
当它遇到第一个if
函数时,编译器会抛出未处理的异常和访问位置写入错误...
如果我删除if
函数并将difficultyLevel
定义为algE仅用于测试相同的问题。
如果我完全删除difficultyLevel
并只打开文件作为"algE.txt"
和"algEAnswers"
,那么一旦代码进入while循环,我会得到同样的问题,但在不同的内存位置。
std::string
? - πάντα ῥεῖ