获取C2039:'swap':不是会员错误,我该如何解决?

时间:2019-12-22 07:47:38

标签: c++

我建立了这个矩阵类:

#pragma once
#include <vector>
#include <algorithm>

template<class T>
class Matrix
{
public:
    Matrix() = default;
    Matrix(Matrix& source);
    Matrix(int _rows, int _cols, const T& fill);
    Matrix(Matrix&& move) noexcept;
    Matrix& operator=(Matrix&& move) noexcept;          
    ~Matrix() noexcept {};


    // Get row and column sizes
    int get_rows() { return this->rows; }
    int get_cols() { return this->cols; }
    void print() const;

    // Member element access
    T& operator()(const int& row, const int& col);
    const T& operator()(const int& row, const int& col) const;


private:
    int rows, cols;
    std::vector<std::vector<T>> mat;

};


template<class T>
Matrix<T>::Matrix(Matrix<T>& source)
{
    mat = source.mat;
    rows = source.get_rows();
    cols = source.get_cols();
}

template<class T>
Matrix<T>::Matrix(int _rows, int _cols, const T& fill)
{
    mat.resize(_rows);
    for (unsigned i = 0; i < mat.size(); ++i)
        mat[i].resize(_cols, fill);

    rows = _rows;
    cols = _cols;
}

template<class T>
Matrix<T>::Matrix(Matrix&& move) noexcept
{
    move.swap(*this);
}

template <class T>
Matrix<T>& Matrix<T>::operator=(Matrix<T>&& move) noexcept {
    move.swap(*this);
    return *this;
}

template<class T>
void Matrix<T>::print() const
{
    for (int i = 0; i < rows; ++i)
    {
        for (int j = 0; j < cols; ++j)
        {
            std::cout << mat[i][j] << " ";
        }
        std::cout << "\n";
    }
    std::cout << "\n";
}

// Access the individual elements                                                                                                                                             
template<typename T>
T& Matrix<T>::operator()(const int& row, const int& col) {
    return this->mat[row][col];
}

// Access the individual elements (const)                                                                                                                                     
template<typename T>
const T& Matrix<T>::operator()(const int& row, const int& col) const {
    return this->mat[row][col];
}

我有这样的线性代数类,使用名称空间np

#pragma once
#include "Matrix.h"



namespace np
{

    // Get the transpose of matrix 
    template <class T>
    Matrix<T> transpose(Matrix<T>& mat)
    {
        Matrix<T> result(mat.get_rows(), mat.get_cols(), 0);
        for (int i = 0; i < mat.get_rows(); ++i)
        {
            for (int j = 0; j < mat.get_cols(); ++j)
            {
                result(i, j) = mat(j, i);
            }
        }
        return result;
    }


}

当我尝试编译时:

#include <iostream>
#include "Matrix.h"
#include "LinAlg.h"

using namespace np;

int main()
{

    Matrix<int> mat1(4, 4, 1);
    mat1.print();

    auto mat2 = mat1;
    mat2.print();

    for (int i = 0; i < mat1.get_rows(); ++i)
    {
        for (int j = 0; j < mat2.get_cols(); ++j)
        {
            mat1(i, j) = i + 1;
        }
    }

    mat1.print();
    mat1 = np::transpose(mat1);
    mat1.print();


    std::cin.get();
}

我收到此错误消息:

error C2039: 'swap': is not a member of 'Matrix<int>'

有什么办法解决此问题吗?我不确定为什么会收到此错误或如何解决该错误。任何建议表示赞赏。也许我不能在线性代数类的头文件中定义功能?

1 个答案:

答案 0 :(得分:1)

您至少两次使用swap方法:

template<class T>
Matrix<T>::Matrix(Matrix&& move) noexcept
{
    move.swap(*this);
}

但是此方法未在模板类定义中声明。如果没有int的专业化(我想没有),则使用默认定义,不带swap的定义。

定义您的交换操作,这就是解决方案。