user1913982
user1913982

Reputation: 19

Error when trying to make an object

I'm trying to make a Question object. Question being the class but I'm getting an error which is:

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

I'm trying to make an object so I could put it into a multimap of type <int, Questions>

Here is my code:

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

Upvotes: 1

Views: 97

Answers (3)

billz
billz

Reputation: 45410

Questions q1 = new Questions is incorrect syntax.

From map.insert(pair <int, Questions>(1, q1)); I can see your map value type is Questions object instead of Questions pointer, so it should be

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

Also your variable map is having the same name as std::map which is a STL container, suggest you use another name, for example: question_map;

Edit

To allow << iter->second you need to overload operator<< for Questions type.

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

Upvotes: 1

David G
David G

Reputation: 96800

The first line of your questionScore method is the problem:

Questions q1 = new Questions ...

new x returns a pointer to an x object, therefore q1 should be defined as a pointer.

Questions * q1 = new Questions ...

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110658

The new expression gives you a pointer to the object you've dynamically allocated. You need to do Questions* q1 = new Questions(...);. But if you don't need to dynamically allocate (you end up copying the object into your map) don't bother. Just do Questions q1(...);.

Presumably you will change the following lines (q2, q3, etc.) to match, but as they are they are not doing what you expect. The (..., ..., ...) will evaluate to be the right-most item in this comma-separated list. So your q2 line is equivalent to string q2 = "Boxing";.

Upvotes: 1

Related Questions