c++ - 由于迭代器无法打印 multimap

标签 c++ multimap

嘿,我正在尝试打印我的 multimap 值,但我在这方面遇到了错误

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

错误状态:

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

这是我的其余代码:

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

问题.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';
}

最佳答案

添加运算符<<作为Qeustions的友元,这样它就可以访问Question类的私有(private)成员。

在 Questions.h 中,将 operator<< 声明为非成员函数

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);

在Questions.cpp中,定义运算符<<函数

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

关于c++ - 由于迭代器无法打印 multimap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13943800/

相关文章:

java - 实现 Map<MyObject,ArrayList<MyObject>> 的正确方法

c++ - 等效 C 字符串的不同语法?

c++ - 使用 QT6 在 QVideoWidget 上绘制 QVideoFrame

java - 谷歌 Collection 中是否有不区分大小写的多图

c++ - 确定元素是否包含在多重映射中的最快方法?

c++ - 如何打印具有元素集的多重图

c++ - 什么是新的展示位置?

c++ - 长行整数运算

c++ - 如果不先声明函数,我可以交换变量的值吗?

c++ - equal_range 和 2 重载没有对 'this' 指针的合法转换