单个模板类中的C ++第二个模板

时间:2011-12-16 01:15:36

标签: c++ templates

我正在为我的个人图书馆开发矩阵类。我正在使用模板类(Class T),因为我希望矩阵具有任何数字或布尔格式(并且不想重新键入所有内容)。所以我想做的是让操作符+ =允许标量或矩阵。但是,我希望能够将两个矩阵一起添加,无论它们的数字格式如何(U类)。

我收到以下编译错误。

error: prototype for 'JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U>&)' does not match any in class 'JecMatrix<T>'
error: candidates are: template<class T> template<class U> JecMatrix& JecMatrix::operator+=(const U&)
error:                 template<class T> template<class U> JecMatrix& JecMatrix::operator+=(const JecMatrix<U>&)

有没有人有解决方案?我把下面的整个课程都包括在内了。

#ifndef JECMATRIX_H
#define JECMATRIX_H

#include <typeinfo>

#include <QTime>
#include <QList>

#include <JecLibrary_global.h>
#include <JecUtils.h>

template<class T>
class JECLIBRARYSHARED_EXPORT JecMatrix
{
public:
    JecMatrix();
    JecMatrix(const int& rows, const int& cols);
    JecMatrix(const QList<QList<T> >& sourceMatrix);

    ~JecMatrix();

    JecMatrix<T>& operator=(const JecMatrix<T>& rhs);
    bool operator!=(const JecMatrix<T>& rhs) const;
    bool operator==(const JecMatrix<T>& rhs) const;

    template<class U> JecMatrix<T>& operator+=(const JecMatrix<U> &rhs) throw(QString);
    template<class U> JecMatrix<T>& operator+=(const U& rhs) throw(QString);

};

template<class T>
JecMatrix<T>::JecMatrix()
{
    T var;
    assertNumber(var);
}

template<class T>
JecMatrix<T>::JecMatrix(const int &rows, const int &cols)
{
    for (int r = 0; r < rows; ++r)
    {
        matrix.append(QList<T>());
        for (int c = 0; c < cols; ++c)
        {
            matrix[r].append(0);
        }
    }
}

template<class T>
JecMatrix<T>::JecMatrix(const QList<QList<T> >& sourceMatrix)
{
    for (int r = 0; r < sourceMatrix.length(); ++r)
    {
        matrix.append(QList<T>());
        for (int c = 0; c < sourceMatrix.at(r).length(); ++c)
        {
            matrix[r].append(sourceMatrix.at(r).at(c));
        }
    }
}

template<class T>
JecMatrix<T>& JecMatrix<T>::operator=(const JecMatrix<T>& rhs)
{
    if (this == &rhs)
    {
        return *this;
    }


    this->id = rhs.id;                          // The id of the Base.

    this->minerals = rhs.minerals;              // The minerals available at that base.
    this->vespene = rhs.vespene;                        // The gas available at that base.

    this->buildings = rhs.buildings;            // The units the player owns during this second. The outer QList holds the types of units and the inner holdes the unique coppies of that unit.
    this->units = rhs.units;                    // The units the player owns during this second. The outer QList holds the types of units and the inner holdes the unique coppies of that unit.

    return *this;
}


template<class T,class U>
JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U> &rhs) throw(QString)
{
    // Perform type checking.
    U var;
    assertNumber(var);

    // Perform size checking.
    if (rhs.getRows() != getRows()) {
        throw ("To add the matrices they must have the same number of rows and columns.  Matrix a has "
                    + QString::number(a.getRows())
                    + " rows and matrix b has "
                    + QString::number(b.getRows()) + " rows.");
    }

    if (rhs.getCols() != getCols()) {
        throw ("To add the matrices they must have the same number of rows and columns.  Matrix a has "
                    + QString::number(a.getCols())
                    + " cols and matrix b has "
                    + QString::number(b.getCols()) + " cols.");
    }

    double result[][] = new double[a.getRows()][a.getCols()];

    // Add the matrices.
    for (int resultRow = 0; resultRow < a.getRows(); resultRow++) {
        for (int resultCol = 0; resultCol < a.getCols(); resultCol++)
        {
            matrix[resultRow][resultCol] += rhs.matrix[resultRow][resultCol];
        }
    }

    return *this;
}

template<class T, class U>
JecMatrix& JecMatrix::operator+=(const U& rhs) throw(QString)
{
    // Perform type checking.
    U var;
    assertNumber(var);

    // Perform the scalar addition.
    for (int r = 0; r < matrix.length(); ++r)
    {
        for (int c = 0; c < matrix.at(0).length(); ++c)
        {
            matrix[r][c] += rhs;
        }
    }
}


#endif // JECMATRIX_H

编辑:删除了不相关的代码。

1 个答案:

答案 0 :(得分:2)

这样说:

template <class T>
template <class U>
JecMatrix<T>& JecMatrix<T>::operator+=(const JecMatrix<U> &rhs) throw(QString)
{
    // ...
}

两个模板声明必须分开。