使用:MSVS2012C++:访问冲突写入位置
代码
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循环。
什么是'difficultyLevel'到底是什么? – 2014-10-20 14:48:39
该参数应该在函数内用assert而不是outside来验证。如果问题是一个全局变量,我不知道你为什么试图返回它。 – 2014-10-20 14:49:31
[MCVE](http://stackoverflow.com/help/mcve)或者它没有发生。 – Angew 2014-10-20 14:49:33