0

我尝试使用模板来定义一些类,如下所示:

。H

template <class T>
class QVecTemp
{
public:
    static QVector<QVector<T>> SplitVector<T>(QVector<T>, int);
private:
};

.cpp

    #include "QVecTemp.h"

template <class T>
QVector<QVector<T>> QVecTemp::SplitVector<T>(QVector<T> items, int clustNum)
{
    QVector<QVector<T>> allGroups = new QVector<QVector<T>>();

    //split the list into equal groups
    int startIndex = 0;
    int groupLength = (int)qRound((float)items.count() / (float)clustNum);
    while (startIndex < items.count())
    {
        QVector<T> group = new QVector<T>();
        group.AddRange(items.GetRange(startIndex, groupLength));
        startIndex += groupLength;

        //adjust group-length for last group
        if (startIndex + groupLength > items.count())
        {
            groupLength = items.Count - startIndex;
        }

        allGroups.Add(group);
    }

    //merge last two groups, if more than required groups are formed
    if (allGroups.count() > clustNum && allGroups.count() > 2)
    {
        allGroups[allGroups.count() - 2].append(allGroups.last());
        allGroups.remove(allGroups.count() - 1);
    }

    return (allGroups);
}

static QVector<QVector<T>> SplitVector<T>(QVector<T>, int);我收到以下错误:

错误:嵌套模板参数列表中的“>>”应该是“> >”

错误:预期的';' 在成员声明结束时

错误:“<”标记之前的预期不合格 ID

究竟是什么问题?我该如何解决?

4

3 回答 3

2
static QVector<QVector<T> > SplitVector(QVector<T>, int);

你有两个错误。

首先,在 C++03 中,>>不能用于一次关闭两个模板参数列表,因为>必须用于关闭一个参数列表并且>>是与两个不同的标记>。在 C++11 中,此问题已得到修复。编译器为此给了你一个很棒的错误信息:>>应该是> >.

其次,类模板的成员函数在其名称后没有类的模板参数。它们不是模板专业化。编译器为此给了您一个可怕的消息:在它看到名称SplitVector后发现 a <,它不存在(因为SplitVector不是已知的模板名称),因此编译器假定您正在声明一个名为的数据成员SplitVector并且 a;必须完成声明(这是第一条错误消息)。然后它再次查看,但<仍然无法理解它,并给你一个更糟糕的信息,假装任何可能出现在代码中的任何东西都必须是某种名称。

于 2013-08-21T14:32:36.183 回答
0
static QVector<QVector<T> > SplitVector<T>(QVector<T>, int);
于 2013-08-21T14:19:50.887 回答
0

您的编译器非常简洁地告诉您问题所在:在 C++11 之前,您不能编写以下内容:

some_class<other_class<int>> x; // error

因为编译器会>>将该行中的嵌套解释为>>运算符。你需要一个额外的空间:

some_class<other_class<int> > x; // this is ok

请注意,较新的编译器(支持 C++11)不再存在此问题。

于 2013-08-21T14:32:04.840 回答