2012-12-19 29 views
0

嘿我試着打印出我的多重映射值,但IM得到一個錯誤,在這個廣東話打印多重映射,因爲迭代

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 

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

那麼,如果'的ostream&運算<< (const ostream&out,const Questions&q)'不可用(它現在已被註釋掉),所以不能使用它。 – Zeta

回答

2

添加操作< <爲Qeustions的朋友,所以它可以訪問私有Question類的成員。

在Questions.h,操作者申報< <作爲非成員函數

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

,定義operator < <功能

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

感謝billz我欣賞它:)。 – user1913982

+0

完全沒問題。 – billz

0

您尚未定義用於流式傳輸Question類型對象的功能。

您需要定義函數:

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

這是我得到的錯誤:錯誤錯誤LNK2019:無法解析的外部符號「class std :: basic_ostream >&__cdecl operator <<(class std :: basic_ostream user1913982

0

看起來你有重載< <運營商的問題類:

聲明:

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

定義:

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

我是否將這個問題放在questions.cpp中? – user1913982

+0

聲明將在你的.h和你的定義中進行。CPP – Foggzie

+0

事實上,你已經擁有了它在最後一行註釋掉下市民: – Foggzie

0

的錯誤信息是很直接告訴你這個問題:你想插入Questions類型的對象到流,但你:

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

只出現在註釋中。你需要編寫一些代碼。此外,它不能成爲課程類的成員。通常,這將是一個全球性的重載的類的朋友,所以在類定義內你會改變它的東西,如:

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