编译器找不到模板化方法的实例

时间:2018-11-24 11:05:51

标签: c++ qt

我正在尝试为我编写的选择类编写一个方法,该方法将扫描可以转换为给定数据类型的第一项的选择列表并返回它(如果没有,则返回null)。不幸的是,我的编译器在抱怨:

error: undefined reference to `GraphNode* Selection::getFirstItem<GraphNode*>()'

编辑器提供了一些提示:

warning: instantiation of function 'Selection::getFirstItem<GraphNode *>' required here, but no definition is available
forward declaration of template entity is here
add an explicit instantiation declaration to suppress this warning if 'Selection::getFirstItem<GraphNode *>' is explicitly instantiated in another translation unit

这是我选择课程的相关部分:

//Selection.h
class Selection : public QObject
{
    QList<QObject*> _items;
public:

    template <class T> T getLastItem();
}

//Selection.cpp
template <class T> T Selection::getLastItem()
{
    for (QObject* obj: _items)
    {
        T val = qobject_cast<T>(obj);
        if (val)
            return val;
    }
    return nullptr;
}

这是发生错误的行:

GraphNode* top = _curScene->selection().getLastItem<GraphNode*>();

是否可以编写此代码,以便编译器能够找到我的getLastItem()方法的实现?

0 个答案:

没有答案