【发布时间】:2012-12-19 08:47:53
【问题描述】:
我正在尝试创建一个 Question 对象。 Question 是班级,但我收到一个错误:
错误 1 错误 C2440:“正在初始化”:无法从
Questions *转换为Questions
我正在尝试制作一个对象,以便可以将其放入 multimap 类型的 <int, 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';
}
【问题讨论】:
-
q1应该是一个指针,这样声明它Questions *q1。 -
你所有的字符串都将是最右边的值。另外,如果您需要指针,请不要使用
new。请改用智能指针。 -
您在 q9 字符串中使用双引号的方式是错误的。将
""Gladiator""替换为\"Gladiator\"。