2012-12-18 60 views
1

我正在嘗試製作一個Question對象。 Question作爲類,但我得到一個錯誤是:嘗試製作對象時出錯

Error 1 error C2440: 'initializing' : cannot convert from Questions * to Questions

我試圖讓一個對象,我就可以把它放到類型<int, Questions>

這裏的multimap是我的代碼:

#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 = new Questions("Whats the oldest known city in the world?", "Sparta", "Tripoli", "Rome", "Demascus"); 
    string q2 = ("What sport in the olympics are beards dissallowed?", "Judo", "Table Tennis", "Volleyball", "Boxing"); 
    string q3 = ("What does an entomologist study?", "People", "Rocks", "Plants", "Insects"); 
    string q4 = ("Where would a cowboy wear his chaps?", "Hat", "Feet", "Arms", "Legs"); 
    string 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"); 

    map.insert(pair <int, Questions>(1, q1)); 
    map.insert(pair <int, string>(2, q2)); 
    // map.insert(pair<int,string>(3, q3)); 
    for (multimap <int, string, std::less <int> >::const_iterator iter = map.begin(); iter != map.end(); ++iter) 
     cout << iter->first << '\t' << iter->second << '\n'; 
} 
+0

'q1'應該是一個指針,用這種方式聲明'Questions * q1'。 – imreal

+0

所有的字符串都將是最右邊的值。另外,如果你需要一個指針,請不要使用'new'。改用智能指針。 – chris

+0

你在q9字符串中使用雙引號的方式是錯誤的。用'\「Gladiator \」'替換'「」角鬥士「」''。 –

回答

1

Questions q1 = new Questions是不正確的語法。

map.insert(pair <int, Questions>(1, q1));我可以看到你的map值類型問題對象,而不是問題的指針,所以它應該是

Questions q1 = Questions ("Whats the oldest known city in the world?", "Sparta" , "Tripoli" , "Rome", "Demascus"); 

而且你的變量映射具有相同名稱的std ::地圖是一個STL容器,建議您使用其他名稱,例如:question_map;

編輯

爲了讓您<< iter->second需要重載operator<<的問題類型。

std::ostream& operator<<(const std::ostream& out, const Questions& q) 
{ 
    out << q.question; // I made up this member as I can't see your Questions code 
    return out; 
} 
+0

當我改變,你可以看到一個問題,這條線cout << iter->第一<< << \ n'<< iter->第二<< << \ n'; – user1913982

+0

iter是迭代器,用法是正確的。 – billz

+0

我得到這個錯誤:錯誤錯誤C2679:二進制'<<':沒有找到操作符需要'const Questions'類型的右側操作數(或沒有可接受的轉換) – user1913982

1

new表達式爲您提供了一個指向您動態分配的對象的指針。你需要做Questions* q1 = new Questions(...);。但是,如果您不需要動態分配(最終將對象複製到地圖中),請勿打擾。只要做Questions q1(...);

大概你會改變以下幾行(q2,q3等)以匹配,但是因爲它們不符合你的期望。 (..., ..., ...)將評估爲此逗號分隔列表中最右邊的項目。所以你的q2行相當於string q2 = "Boxing";

0

questionScore方法的第一行的問題是:

Questions q1 = new Questions ... 

new x返回一個指向一個x對象,因此q1應該被定義爲一個指針。

Questions * q1 = new Questions ... 
相關問題