zeboidlund
zeboidlund

Reputation: 10137

Passing reference to QPixmap ctor

When I try to pass what appears to be a reference to my QPixmap, I get an error:

 error: no matching function for call to ‘QGraphicsScene::addItem(QGraphicsPixmapItem (&)(QPixmap))’

The problem is that I don't know where this reference is coming from, though I'm guessing it's coming from the iterator.

void MainWindow::addItems(std::map<std::string, QString> * items)
{
    std::map<std::string, QString>::const_iterator current;

    for(current = items->begin(); current != items->end(); ++current)
    {
        QString cur = current->second;
        QGraphicsPixmapItem item(QPixmap(cur));
        _scene->addItem(item);

    }

}

If that's the case, is there a way to de-reference the iterator? Otherwise, what is it that I'm doing wrong?

The code which calls it

int main(int argc, char *argv[])
{
    std::map<std::string, QString> * items;

    items->insert(std::pair<std::string, QString>("ozone", ":/images/ozone_sprite.png"));

    QApplication a(argc, argv);
    MainWindow window;
    window.addItems(items);
    window.show();

    delete items;

    return a.exec();
}

Upvotes: 1

Views: 414

Answers (1)

Stuart Golodetz
Stuart Golodetz

Reputation: 20646

You've fallen foul of what's known as C++'s 'most vexing parse'. Specifically, this:

QGraphicsPixmapItem item(QPixmap(cur));

declares a function called item that takes a single parameter of type QPixmap and returns a QGraphicsPixmapItem. To fix this, write:

QPixmap temp(cur);
QGraphicsPixmapItem item(temp);

See here:

http://en.wikipedia.org/wiki/Most_vexing_parse

As far as the error goes, note that you're trying to call addItem with an argument of type QGraphicsPixmapItem (&)(QPixmap) - that is, reference to a function taking a QPixmap and returning a QGraphicsPixmapItem (which is the type of the expression item).

Upvotes: 1

Related Questions