2012-12-18 12 views
1

Question 객체를 만들려고합니다. Question 클래스 인하지만 오류 받고 있어요 : 나는 여기객체를 만들 때 오류가 발생했습니다.

유형 <int, Questions>multimap에 넣어 수 있도록 내가 객체를 만들기 위해 노력하고있어

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

은 내 코드입니다 :

#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'은 이런 식으로'Question * q1'을 선언하는 포인터 여야합니다. – imreal

+0

모든 문자열이 가장 오른쪽 값이 될 것입니다. 또한 포인터가 필요한 경우'new'를 사용하지 마십시오. 대신 스마트 포인터를 사용하십시오. – chris

+0

q9 문자열에서 큰 따옴표를 사용하는 방식이 잘못되었습니다. 검투사 ""를'검투사 \ "로 대체하십시오. –

답변

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

또한 당신의 변수지도가 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

내가 그걸로 바꿀 때이 줄에 문제가있는 것을 볼 수있다. << iter-> first << '\ t'<< iter-> second << '\ n'; – user1913982

+0

iter는 반복자이므로 사용법이 정확합니다. – billz

+0

임이 오류가 발생했습니다 : 오류 오류 C2679 : 바이너리 '<<': 'const 질문'유형의 오른쪽 피연산자를 사용하는 연산자가 없습니다 (또는 허용되는 변환이 없음) – user1913982

1

new 표현식은 동적으로 할당 한 객체에 대한 포인터를 제공합니다. Questions* q1 = new Questions(...);해야합니다. 그러나 동적으로 할당 할 필요가 없다면 (객체를지도에 복사하는 것을 끝내십시오) 걱정하지 마십시오. 그냥 Questions q1(...);하십시오.

아마도 다음 라인 (q2, q3 등)을 일치시킬 수 있지만 예상대로 수행하지는 못합니다. (..., ..., ...)은이 쉼표로 구분 된 목록에서 맨 오른쪽 항목으로 평가됩니다. 따라서 q2 행은 string q2 = "Boxing";과 같습니다.

Questions q1 = new Questions ... 

new x 따라서 q1는 포인터로 정의되어야한다, x 객체에 대한 포인터를 반환 :

0

당신의 questionScore 방법의 첫 번째 줄은 문제입니다.

Questions * q1 = new Questions ... 
관련 문제