Operator =不能使用std :: vector c ++

时间:2015-04-08 08:15:36

标签: c++ vector operator-keyword

我有一个班级

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;
};

操作员*无法正常工作。在运算符内构建辅助矩阵是可以的,但是当返回它时,它只改变接收矩阵中的整数数据字段。原来问题是operator = for 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;
}

1 个答案:

答案 0 :(得分:0)

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

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

相关问题