【问题标题】:Error when trying to make an object尝试制作对象时出错
【发布时间】: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\"

标签: c++ stl multimap


【解决方案1】:

Questions q1 = new Questions 语法不正确。

map.insert(pair &lt;int, Questions&gt;(1, q1)); 我可以看到你的map 值类型是 Questions 对象而不是 Questions 指针,所以应该是

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

另外你的变量 map 与 std::map 同名,std::map 是一个 STL 容器,建议你使用另一个名字,例如:question_map;

编辑

要允许&lt;&lt; iter-&gt;second,您需要为问题类型重载operator&lt;&lt;

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

【讨论】:

  • 当我改成这个时,你能看到这条线 cout first second
  • 我收到此错误:错误 1 ​​错误 C2679:二进制“
  • 其实这是另一个问题。它与`second`有关,但您还没有为问题类型重载运算符
  • 我知道我是个菜鸟,但我该怎么做呢?
  • 我在标题中声明了它,它说我有太多参数。
【解决方案2】:

new 表达式为您提供了一个指向您已动态分配的对象的指针。你需要做Questions* q1 = new Questions(...);。但是,如果您不需要动态分配(最终将对象复制到地图中),请不要打扰。只需Questions q1(...);

您可能会更改以下几行(q2q3 等)以匹配,但实际上它们并没有按照您的预期进行。 (..., ..., ...) 将评估为此逗号分隔列表中最右边的项目。所以你的q2 行等同于string q2 = "Boxing";

【讨论】:

    【解决方案3】:

    questionScore 方法的第一行是问题所在:

    Questions q1 = new Questions ...
    

    new x返回一个指向x对象的指针,因此q1应该被定义为一个指针。

    Questions * q1 = new Questions ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-25
      • 2020-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多