我目前正在创建一个帮助我学习新词汇的程序。该程序的工作流程将由VocabularyTester类统治,它包含存储在矢量数组中的WordPair类对象列表。
class VocabularyTester
{
private:
std::vector<WordPair> wordList;
std::vector<WordPair> failedPairs;
std::vector<WordPair> passedPairs;
WordPair recentPair;
public:
VocabularyTester();
void run();
void loadWordList(std::wstring& vocabularyRaw);
/*
other parts of the code
*/
};
/* the function i'm having a problem with */
void VocabularyTester::loadWordList(std::wstring& vocabularyRaw)
{
std::wstring rawPair;
unsigned int iterator = 0, prevIterator = 0;
for (;;)
{
iterator = vocabularyRaw.find(L"\r\n", iterator);
if (iterator == std::string::npos)
{
break;
}
rawPair = vocabularyRaw.substr(prevIterator, iterator);
wordList.emplace_back(rawPair); /*there occurs the read access violation exception*/
prevIterator = iterator;
}
}
当执行到达wordList.emplace_back(rawPair)时,抛出读取访问冲突异常,正好在这里:
/* vector header file */
bool _Has_unused_capacity() const _NOEXCEPT
{ // micro-optimization for capacity() != size()
return (this->_Myend() != this->_Mylast());
}
WordPair是一对2个单词,它有2个字符串成员:leftWord用于母语单词,rightWord用于某个单词想要学习的单词。这是我用来将它添加到矢量数组的对象的构造函数:
WordPair::WordPair(std::wstring& rawPair)
{
std::wstring separator = L" - ";
size_t separatorPosition = rawPair.find(separator);
std::wstring leftWord = rawPair.substr(0, separatorPosition);
std::wstring rightWord = rawPair.substr(separatorPosition + separator.length());
this->leftWord = leftWord;
this->rightWord = rightWord;
}
感谢您的帮助。