2012-12-19 3 views
0

Hey Im essayant d'imprimer mes valeurs multimap mais im obtenir une erreur sur cetteCant impression multimap en raison de iterator

for (multimap< int, Questions, less<int> >::iterator iterator = question_map.begin(); 
     iterator != question_map.end(); ++iterator) 
     cout << iterator->first << '\t' << iterator->second << '\n'; 

Les états d'erreur:

Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'Questions' (or there is no acceptable conversion) c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\millionaire\millionaire\questions.cpp 31 1 Millionaire 

Voici le reste de mon code :

Questions.h 
#ifndef QUESTIONS_H 
#define QUESTIONS_H 
#include <string> 
#include <algorithm> 
#include <map> 
#include <iostream> 
using namespace std; 

class Questions 
{ 
public: 
    Questions(); 
    Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3); 
    //void shuffle(string *array, int n); 
    //string getQuestion(); 
    //string getCorrectAnswer(); 
    //string getAnswers(); 
    //bool checkAnswer(string answer); 
    void questionStore(); 
    //void addQuestion(int level, Questions question); 
    //Questions printQuestion(int level); 
    //ostream& operator<<(const ostream& out, const Questions& q); 


private: 
    string question; 
    string correctAnswer; 
    string wrongAnswer1; 
    string wrongAnswer2; 
    string wrongAnswer3; 
    multimap<int,Questions> question_map; 
}; 

#endif 

Questions.cpp

#include <iostream> 
#include "Questions.h" 
using namespace std; 
Questions :: Questions() 
{ 

} 
Questions :: Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3) 
{ 
} 


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

question_map.insert(pair<int,Questions>(1,q1)); 
question_map.insert(pair<int,Questions>(1,q2)); 
question_map.insert(pair<int,Questions>(1,q3)); 

for (multimap< int, Questions, less<int> >::iterator iterator = question_map.begin(); 
     iterator != question_map.end(); ++iterator) 
     cout << iterator->first << '\t' << iterator->second << '\n'; 
} 
+0

Eh bien, si 'ostream & operator << (const ostream & out, const Questions & q) 'n'est pas disponible (il est actuellement commenté) vous ne pouvez pas l'utiliser. – Zeta

Répondre

2

Ajouter opérateur < < comme ami de Qeustions, il peut accéder privé membre de la classe Question.

En Questions.h, déclarer opérateur < < en fonction non-membre

class Questions 
{ 
public: 
    Questions(); 
    Questions(string question,string correctAnswer, string wrongAnswer1,string wrongAnswer2,string wrongAnswer3); 
    void questionStore(); 
private: 
    string question; 
    string correctAnswer; 
    string wrongAnswer1; 
    string wrongAnswer2; 
    string wrongAnswer3; 
    multimap<int,Questions> question_map; 

    friend std::ostream& operator<<(std::ostream& stream, Questions const& q); 
}; 

std::ostream& operator<<(std::ostream& stream, Questions const& q); 

dans Questions.cpp, définir opérateur < < fonction

std::ostream& operator<<(std::ostream& stream, Questions const& q) 
{ 
    stream << q.question << " " << q.correctAnswer; 
    return stream; 
} 
+0

Merci billz je l'apprécie :). – user1913982

+0

pas de problème du tout. – billz

0

Vous n'avez pas défini la fonction de diffusion d'un objet de type Question.

Vous devez définir la fonction:

std::ostream& operator<<(std::ostream& stream, Question const& question); 
+0

Ceci est l'erreur im get: Erreur erreur LNK2019: symbole externe non résolu "classe std :: basic_ostream > & __cdecl opérateur << (classe std :: basic_ostream > &, classe Questions const &) "(?? 6 @ YAAAV? $ basic_ostream @ DU? $ char_traits @ D @ std @@@ std @@ AAV01 @ ABVQuestions @@@ Z) référencé dans la fonction" public : void __thiscall Questions :: questionStore (void) "(? questionStore @ Questions @@ QAEXXZ) – user1913982

0

semble que vous devez surcharger le < < opérateur pour votre classe Questions:

Déclaration:

friend ostream &operator<<(ostream &out, const Question& q); 

Définition:

ostream &operator<<(ostream &out, const Question& q) 
{ 
    out << q.question; 
    return out; 
} 
+0

Est-ce que je peux mettre ceci dans les questions.cpp? – user1913982

+0

La déclaration ira dans votre .h et la définition dans votre.cpp – Foggzie

+0

En fait, vous l'avez déjà commenté dans la dernière ligne sous public: – Foggzie

0

Le message d'erreur est vous dire le problème tout à fait directement: vous essayez d'insérer un objet de type Questions dans le flux, mais votre:

//ostream& operator<<(const ostream& out, const Questions& q); 

est seulement présente comme un commentaire. Vous devez écrire du code. En outre, il ne peut pas être membre de la classe Questions. En général, il sera une surcharge globale qui est un ami de la classe, donc dans la définition de la classe que vous changeriez à quelque chose comme:

friend ostream &operator<<(ostream &out, const questions &q) { 
    return out << d.question; // probably other fields here 
}