传递对QPixmap ctor的引用

时间:2011-12-08 00:21:30

标签: c++ qt pointers reference pass-by-reference

当我尝试传递似乎是对QPixmap的引用时,我收到错误:

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

问题是我不知道这个引用来自哪里,虽然我猜它是来自迭代器。

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

    }

}

如果是这种情况,有没有办法取消引用iterator?否则,我做错了什么?

调用它的代码

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

1 个答案:

答案 0 :(得分:1)

你已经违背了所谓的C ++的“最令人烦恼的解析”。具体来说,这个:

QGraphicsPixmapItem item(QPixmap(cur));

声明一个名为item的函数,该函数接受QPixmap类型的单个参数并返回QGraphicsPixmapItem。要解决此问题,请写下:

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

见这里:

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

就错误而言,请注意您尝试使用addItem类型的参数调用QGraphicsPixmapItem (&)(QPixmap) - 也就是说,引用一个函数QPixmap并返回QGraphicsPixmapItem(表达式item的类型)。