运算符= 不适用于 std::vector c++

Operator= doesn't work with std::vector c++

本文关键字:vector c++ std 适用于 不适用 运算符      更新时间:2023-10-16

我有一个类

template <class T>
class General_matrix : public Math_object<T>
{
    public:
        General_matrix(const size_t m, const size_t n);
        ~General_matrix();
        void init();
        void show();
        T* operator()(const size_t, const size_t) const;
        General_matrix<T> operator*(const General_matrix<T>&);
};

源自

template <class T>
class Math_object
{
    protected:  
        size_t size_m, size_n;
        std::vector <T> field;
    public:
        Math_object(const size_t m, const size_t n);
        virtual ~Math_object() = 0;
        virtual void show() = 0;
        virtual void init() = 0;
};

操作员*工作不正常。在运算符中构建辅助矩阵是可以的,但当返回时,它只会改变接收矩阵中的整数数据字段。事实证明,问题出在std::vector的运算符=上。我不想推翻它。其他人遇到过这个问题吗?为什么我不能将向量分配给相同大小的向量?

template <typename T>
General_matrix<T> General_matrix<T>::operator*(const General_matrix<T> &right_operand)
{
    General_matrix<T> aux(this->size_m, right_operand.size_n);
    T collector;
    for (int i=0; i<this->size_m; i++)
    {
        for (int j=0; j<right_operand.size_n; j++)
        {
            collector = 0;
            for (int k=0; k<this->size_n; k++)
            {
                 collector += *((*this)(i,k)) * *(right_operand(k,j));
            }
            *(aux(i,j)) = collector;
        }
    }
    return aux;
}

UPD:这是MCVE有两个矩阵的值分别为{{0,1,2}、{3,4,5}}和{0,1}、{2,3}、{4,5}。它们相乘的结果必须是{{10,13},{28,40}},并且是{0,0},{0,0}}。

#include <iostream>
#include <vector>
using namespace std;
template <class T>
class Math_object
{
    protected:
        size_t size_m, size_n;
        std::vector <T> field;
    public:
        Math_object(const size_t m, const size_t n);
        virtual ~Math_object() = 0;
        virtual void show() = 0;
        virtual void init() = 0;
};
template <class T>
class General_matrix : public Math_object<T>
{
    public:
        General_matrix(const size_t m, const size_t n);
        ~General_matrix();
        void init();
        void show();
        T* operator()(const size_t, const size_t) const;
        General_matrix<T> operator*(const General_matrix<T>&);
};
template <typename T>
Math_object<T>::Math_object(const size_t m, const size_t n/*=1*/)
{
    cout << "Constructor MO"<<"n";
    size_m=m;
    size_n=n;
    field.reserve(m*n);
}
template <typename T>
Math_object<T>::~Math_object()
{
    cout << "Destructor MO"<<"n";
    vector<T>().swap(this->field);
}
template <typename T>
void Math_object<T>::show() {};

template <typename T>
General_matrix<T>::General_matrix(const size_t m, const size_t n):Math_object<T>(m,n)
{
    cout << "Constructor GM"<<"n";
}
template <typename T>
General_matrix<T>::~General_matrix()
{
    cout << "Destructor GM"<<"n";
}
template <typename T>
void General_matrix<T>::init()
{
    cout << "Input matrix"<<"n";
    for (int i=0; i<this->size_m*this->size_n; i++)
    this->field[i] = i;
}
template <typename T>
T* General_matrix<T>::operator()(const size_t i, const size_t j) const
{
    return const_cast<T*>(&(this->field[i*(this->size_n)+j]));
}
template <typename T>
void General_matrix<T>::show()
{
    for (int i=0; i < this->size_m; i++)
    {
        for (int j=0; j < this->size_n; j++)
        {
            cout << *((*this)(i,j)) << " ";
        }
        cout << "n";
    }
}
template <typename T>
General_matrix<T> General_matrix<T>::operator*(const General_matrix<T> &right_operand)
{
    General_matrix<T> aux(this->size_m, right_operand.size_n);
    T collector;
    for (int i=0; i<this->size_m; i++)
    {
        for (int j=0; j<right_operand.size_n; j++)
        {
            collector = 0;
            for (int k=0; k<this->size_n; k++)
            {
                collector += *((*this)(i,k)) * *(right_operand(k,j));
            }
            *(aux(i,j)) = collector;
        }
    }
   return aux;
}
template class Math_object<int>;
template class Math_object<float>;
template class General_matrix<int>;
template class General_matrix<float>;
int main()
{
    General_matrix<int> k(2,3);
    k.init();
    General_matrix<int> p(3,2);
    p.init();
    General_matrix<int> t(2,2);
    t=k*p;
    t.show();
    return 0;
}

注释中提到的一个问题是在构造函数中使用reserve而不是resize

但是您在operator()中也有未定义的行为。更好地实现const和非const版本。