Hey im trying to print out from a multimap. My multimap is: multimap<int,Questions*> map;
What i want to happen is when im calling a question: questions.printQuestion(1); Its prints out the 3 questions in random order. But all im getting so far is a run-time error when the printQuestion is called.
Run time error:
Debug Assertion Failed!
Expression: map/set iterator may not be dereferencable
Here is my code:
#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;
}
Can anybody help me with this.
When you say you get a "run-time error" I assume the functions prints and integer and the address of a pointer. You probably meant to dereference the pointer to the question before printing it:
*it->second
Another problem is that you dereference the past-the-end iterator when you return iter->second
: The loop in front of the return
statement is exited when iter == map.end()
. Dereferencing the past the end iterator is illegal and causes undefined behavior (this is the error actually being asserted).
Of course, you should probably store Question
objects in the first place and not allocate them on the heap: They are likely to leak. It also doesn't seem quite right to have a "question" store in a "Question*s*" object: You probably want to have to separate classes: one for an individual question and one collecting multiple questions.