2012-12-18 3 views
-1

안녕하세요, 저는 멀티 맵에서 인쇄하려고합니다. 내 멀티 맵은 다음과 같습니다 : multimap<int,Questions*> map; 나는 무엇이 일어나길 원하니, 질문을하는 순간입니다. questions.printQuestion (1); 3 개의 질문을 무작위로 인쇄합니다. 하지만 printQuestion이 호출 될 때마다 런타임 오류가 발생합니다.멀티 맵에서 인쇄하는 방법

#include <iostream> 
#include "Questions.h" 
using namespace std; 

Questions :: Questions() 
{ 
} 

Questions::Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3) 
{ 
    this->question = question; 
    this->pAnswers = new string[4]; 
    this->pAnswers[0]=wrongAnswer1; 
    this->pAnswers[1]=wrongAnswer2; 
    this->pAnswers[2]=wrongAnswer3; 
    this->pAnswers[3] =correctAnswer; 
    this->shuffle(this->pAnswers,4); 
    this->correctAnswer = correctAnswer; 
} 

void Questions::shuffle(string *array, int n) 
{ 
    random_shuffle(&this->pAnswers[0],&this->pAnswers[4]); 
} 

string Questions::getQuestion() 
{ 
    return this->question; 
} 

string Questions::getCorrectAnswer() 
{ 
    return this->correctAnswer; 
} 

string* Questions::getAnswers() 
{ 
    return this->pAnswers; 
} 

bool Questions::checkAnswer(string answer) 
{ 
    if(this->correctAnswer.compare(answer)==0) 
    { 
     return true; 
    } 
    return false; 
} 

void Questions::questionStore() 
{ 
    Questions *q1 = new Questions("Whats the oldest known city in the world?", "Sparta" , "Tripoli" , "Rome", "Demascus"); 
    Questions *q2 = new Questions("What sport in the olympics are beards dissallowed?", "Judo", "Table Tennis" , "Volleyball", "Boxing"); 
    Questions *q3 = new Questions("What does an entomologist study?", "People" , "Rocks" , "Plants", "Insects"); 
    Questions *q4 = new Questions("Where would a cowboy wear his chaps?", "Hat" , "Feet" , "Arms", "Legs"); 
    Questions *q5 = new Questions("which of these zodiac signs is represented as an animal that does not grow horns?", "Aries" , "Tauris" , "Capricorn", "Aquarius"); 
    Questions *q6 = new Questions("Former Prime Minister Tony Blair was born in which country?", "Northern Ireland" , "Wales" , "England", "Scotland"); 
    Questions *q7 = new Questions("Duffle coats are named after a town in which country?", "Austria" , "Holland" , "Germany", "Belgium"); 
    Questions *q8 = new Questions("The young of which creature is known as a squab?", "Horse" , "Squid" , "Octopus", "Pigeon"); 
    Questions *q9 = new Questions("The main character in the 2000 movie ""Gladiator"" fights what animal in the arena?", "Panther" , "Leopard" , "Lion", "Tiger"); 

    addQuestion(1,q1); 
    addQuestion(1,q2); 
    addQuestion(1,q3); 
    addQuestion(2,q4); 
    addQuestion(2,q5); 
    addQuestion(2,q6); 
    addQuestion(3,q7); 
    addQuestion(3,q8); 
    addQuestion(3,q9); 
} 

void Questions::addQuestion(int level, Questions *question) 
{ 
    map.insert(pair<int,Questions*>(level,question)); 
} 


Questions* Questions::printQuestion(int level) 
{ 
    multimap<int, Questions*>::iterator iter; 
    pair<multimap<int, Questions*>::iterator,multimap<int, Questions*>::iterator> constIter; 
    for (multimap< int, Questions*, less<int> >::const_iterator iter =map.begin(); 
    iter != map.end(); ++iter) 
     cout << iter->first << '\t' << iter->second << '\n'; 



     /*constIter = map.equal_range(level); 
    size_t sz = distance(constIter.first, ret.second); 
    size_t idx = rand(); 
    if(ret.first != ret.second) 
    advance(ret.first, idx); 
    it =ret.first; 
    Questions* question = (*it).second; 
    return (*it).second; 
     cout << question->getQuestion() << std::endl;*/ 
     return iter->second; 
} 

이 사람이 나를 도울 수 :

Run time error: 
Debug Assertion Failed! 
Expression: map/set iterator may not be dereferencable 

여기 내 코드입니다.

+0

오류가 표시 될 수 있습니까? – Foggzie

+0

예, 확실하게 편집하겠습니다. – user1913982

답변

1

"런타임 오류"가 발생하면 함수가 정수, 포인터 주소를 출력한다고 가정합니다. 당신은 아마 그것을 인쇄하기 전에 질문에 대한 포인터 역 참조로 의미 :

*it->second 

또 다른 문제는 past-the-end 반복자를 역 참조하면 iter->second 돌아올 때이다 다음 return 문 앞의 루프가 종료 될 때 iter == map.end(). 과거를 역 참조하면 최종 반복기가 올바르지 않고 정의되지 않은 동작이 발생합니다 (이것은 실제로 어설트되는 오류입니다).

처음에는 Question 개의 개체를 저장하고 이 아닌 개체는 힙에 할당해야합니다. 누출 가능성이 있습니다. 또한 "Question * s *"개체에 "질문"상점이있는 것이 옳지 않은 것으로 보입니다. 개체를 구분해야합니다. 개별 질문과 여러 질문을 수집하는 것이 좋습니다.

관련 문제