【问题标题】:How to write a friend (operator) method in a template?如何在模板中编写朋友(操作员)方法?
【发布时间】:2019-07-29 15:24:57
【问题描述】:

所以我试图在模板类中重载运算符 +。 代码编译并运行,但在使用运算符 + 时崩溃。 尝试了很多东西,我认为这是一个语法问题?任何意见,将不胜感激!

运算符 = 已重载并且可以工作。

矩阵.h

template <int row, int col, class T = int>
class Matrix
{
    int rows;
    int cols;
    T** mat;

public:

    Matrix(int defVal = 0) {
        rows = row;
        cols = col;
        memory();

        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                mat[i][j] = defVal;
    }
    ~Matrix() {
        del();
    }

    Matrix(const Matrix& other) {
        *this = other;
    }

    const Matrix& operator=(const Matrix& other) {
        if (&other != this)
        {   
            rows = other.rows;
            cols = other.cols;
            del();
            memory();
            for (int i = 0; i < rows; i++)
                for (int j = 0; j < cols; j++)
                    mat[i][j] = other.mat[i][j];
        }
        return *this;
    }

    friend ostream& operator<<(ostream& os, const Matrix& m) {

        for (int i = 0; i < m.cols; i++)
        {
            for (int j = 0; j < m.rows; j++)
                os << m.mat[i][j] << " ";
            os << endl;
        }
        return os;
    }

    friend Matrix operator+(const Matrix& other, T num) {

        Matrix temp = other;

        for (int i = 0; i < temp.rows; i++)
            for (int j = 0; j < temp.cols; j++)
                temp.mat[i][j] += num;
        return temp;
    }

    void memory(){
        mat = new T * [rows];
        for (int i = 0; i < rows; i++)
            mat[i] = new T[cols];
    }

    void del(){
        for (int i = 0; i < rows; i++)
            delete[] mat[i];
        delete[] mat;
    }
};

main.cpp

int main() {

    Matrix<4, 4> mat;
    std::cout << mat << std::endl;

    Matrix<4, 4> identity(1);

    std::cout << identity + 3 << std::endl; //crashes here

return 0;
}

如果您需要代码的其他部分,请告诉我!提前致谢!

【问题讨论】:

  • 我们需要看看Matrix是如何实现的。你有复制构造函数吗?
  • 您的代码无法编译。您错过了operator&lt;&lt;,这可能是难题的重要组成部分。阅读常见问题解答并提供minimal reproducible example
  • 有人可以解释一下这个语法:friend Matrix operator+(我第一次看到这样的东西。
  • @deoncagadoes 是友元函数的声明。如果您还没有使用朋友功能,听起来您可以使用good C++ book

标签: c++ oop templates friend


【解决方案1】:

你的复制构造函数和赋值看起来很可疑,你 del 在更改 rowscols 之后,没有初始化任何东西。 我希望它应该是

Matrix(const Matrix& other) : rows(0), cols(0), mat(nullptr) {
    *this = other;
}

const Matrix& operator=(const Matrix& other) {
    if (&other != this)
    {   
        del(); // clean up first
        rows = other.rows;
        cols = other.cols;
        memory();
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                mat[i][j] = other.mat[i][j];
    }
    return *this;
}

顺便说一句,我根本不会在这里使用动态分配,而是

template <typename T, size_t rows, size_t cols>
class Matrix
{
    std::array<std::array<T, cols>, rows> mat;
public:
    Matrix(T defVal = {}) {
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                mat[i][j] = defVal;
    }

    friend std::ostream& operator<<(std::ostream& os, const Matrix& m) {
        for (int i = 0; i < cols; i++)
        {
            for (int j = 0; j < rows; j++)
                os << m.mat[i][j] << " ";
            os << std::endl;
        }
        return os;
    }

    friend Matrix operator+(Matrix other, T num) {
        for (int i = 0; i < rows; i++)
            for (int j = 0; j < cols; j++)
                other.mat[i][j] += num;
        return other;
    }

    // No need for any special members
};

【讨论】:

  • 好吧,这是家庭作业,所以我必须使用动态内存分配。我修复了复制ctor,它起作用了!你能解释一下你是怎么知道问题的吗?在代码的哪一部分我使用了复制ctor? @Caleth
  • @ShaharStahi 因为你有rowcol 模板参数,你实际上不需要rowscols 作为数据成员。有分配的替代方案是std::unique_ptr&lt;T[][col]&gt; mat;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-27
  • 2016-10-29
  • 2013-09-28
  • 2018-07-15
  • 1970-01-01
  • 2014-05-26
  • 2019-08-08
相关资源
最近更新 更多