Reputation: 19
Hey im trying to print out my multimap values but im getting an error on this
for ( multimap< int, Questions, less< int > >::iterator iterator = question_map.begin();
iterator != question_map.end(); ++iterator )
cout << iterator->first << '\t' << iterator->second << '\n';
The error states:
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
Here is the rest of my 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';
}
Upvotes: 0
Views: 167
Reputation: 45410
Add operator<< as friend of Qeustions, so it can access private member of Question class.
In Questions.h, declare operator<< as a non-member function
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);
in Questions.cpp, define operator<< function
std::ostream& operator<<(std::ostream& stream, Questions const& q)
{
stream << q.question << " " << q.correctAnswer;
return stream;
}
Upvotes: 2
Reputation: 490148
The error message is tell you the problem quite directly: you're trying to insert an object of type Questions
into the stream, but your:
//ostream& operator<<(const ostream& out, const Questions& q);
is only present as a comment. You need to write some code. In addition, it can't be a member of the Questions class. Typically it'll be a global overload that's a friend of the class, so inside the class definition you'd change it to something like:
friend ostream &operator<<(ostream &out, const questions &q) {
return out << d.question; // probably other fields here
}
Upvotes: 0
Reputation: 9821
looks like you have to overload the << operator for your Questions class:
Declaration:
friend ostream &operator<<(ostream &out, const Question& q);
Definition:
ostream &operator<<(ostream &out, const Question& q)
{
out << q.question;
return out;
}
Upvotes: 0
Reputation: 264411
You have not defined the function for streaming an object of type Question
.
You need to define the function:
std::ostream& operator<<(std::ostream& stream, Question const& question);
Upvotes: 0