[英]Matrix Multiplication with operator overloading
I am attempting to create an overloaded operator for a matrix class that I have built. 我正在尝试为我构建的矩阵类创建一个重载运算符。 My matrix class stores the matrix in a dynamically allocated multidimensional array.
我的矩阵类将矩阵存储在动态分配的多维数组中。 I am simply trying to test my overloaded operator by multiplying two matrices together that are exactly the same and display the output.
我只是试图通过将两个完全相同的矩阵相乘并显示输出来测试我的重载运算符。 I am getting weird results and I believe it has to do with one of the conditionals on my for loops.
我得到了奇怪的结果,我相信它与我的for循环中的一个条件有关。 I have however walked through all of my for loops and can find nothing wrong.
然而,我已经遍历了所有的for循环,并且没有发现任何错误。 The matrices I am multiplying together are both 6x6.
我乘以的矩阵都是6x6。
My overloaded operator 我的重载运算符
template <typename T>
const matrix<T> matrix<T>::operator * (const matrix& right) const
{
matrix<T> c = right;
int sum_elems;
for( int i = 0; i < this->rows - 1; ++i)
{
for(int j = 0; j < right.cols - 1; ++j)
{
sum_elems = 0;
for( int k = 0; k < right.rows - 1; ++k)
{
sum_elems += this->the_matrix[i][k] * right.the_matrix[k][j];
}
c.the_matrix[i][j] = sum_elems;
}
}
return c;
}
Now my call to the overloaded operator in my main function: 现在我调用main函数中的重载操作符:
std::cout << my_matrix;
matrix<int> copy_matrix;
copy_matrix = my_matrix * my_matrix;
std::cout << copy_matrix;
My output: 我的输出:
The Matrix:
0 1 0 1 1 0
1 0 1 0 1 1
0 1 0 1 0 1
1 0 1 0 1 0
1 1 0 1 0 1
0 1 1 0 1 0
The Matrix:
-1 33 139587680 18 38 75
139587680 18 38 75 157 1
139587712 38 1470 4365 10411 1
139587744 75 4365 19058932 64514866 0
139587776 157 10411 64514866 1136204102 1
139596144 1 1 0 1 0
As you can see it seems that I am stepping out of bounds on one of my arrays. 正如你所看到的那样,我似乎走出了我的一个阵列的界限。 I can not seem to find where though.
我似乎无法找到它。 I appreciate your help in advance.
我提前感谢您的帮助。
Edit: As requested my full implementation of my matrix class 编辑:按要求我完整实现我的矩阵类
Matrix Definitions: 矩阵定义:
template <typename T>
class matrix
{
public:
//Default Constructor
matrix();
//Overloaded Constructor
matrix(std::ifstream&, const char*);
//Copy Constructor
matrix(const matrix&);
//Destructor
~matrix();
//overloaded operators
T* operator [] (T);
const matrix operator * (const matrix&) const;
matrix& operator = (const matrix&);
friend std::ostream& operator << <T> (std::ostream&, const matrix<T>&);
private:
T** the_matrix;
unsigned rows, cols;
Matrix Implementation: 矩阵实施:
/* Template version of matrix class */
/*---------------------------------------------------------------------------*/
// Default contructor
template <typename T>
matrix<T>::matrix() { }
// Overloaded contructor
template <typename T>
matrix<T>::matrix( std::ifstream& in, const char* file)
{
// declare the variables to be used
T vertices, edges, u, v;
std::string line;
// open file for reading
in.open(file);
// get number of vertices
in >> vertices;
// throw away second line
std::getline(in, line);
std::getline(in, line);
// get number of edges and dump them in two arrays
in >> edges;
T edge1 [edges];
T edge2 [edges];
int j = 0, k = 0;
for(int a = 0; a < edges; ++a)
{
in >> u >> v;
edge1[j] = u;
edge2[k] = v;
++j;
++k;
}
in.close();
// Create multi-dim-dynamic array
rows = vertices;
cols = vertices;
the_matrix = new T*[rows];
for( int b = 0; b < rows; ++b)
{
the_matrix[b] = new T [rows];
}
// Initialize array values to zero
for ( int c = 0; c < rows; ++c)
{
for( int d = 0; d < cols; ++d)
{
the_matrix[c][d] = 0;
}
}
// push the edges to the matrix
for( int e = 0; e < edges; ++e)
{
the_matrix[edge1[e] - 1][edge2[e] - 1] = 1;
}
for ( int f = 0; f < edges; ++f)
{
the_matrix[edge2[f] - 1][edge1[f]-1] = 1;
}
}
// Copy Constructor
template <typename T>
matrix<T>::matrix(const matrix& left)
{
the_matrix = left.the_matrix;
rows = left.rows;
cols = left.cols;
spath = left.spath;
}
// Destructor
template <typename T>
matrix<T>::~matrix()
{
// Deletes the data in reverse order of allocation
for( int a = cols; a > 0; --a)
{
delete[ ] the_matrix[a];
}
delete[ ] the_matrix;
}
// Overloaded * Operator
template <typename T>
const matrix<T> matrix<T>::operator * (const matrix& right) const
{
matrix<T> c = right;
T sum_elems;
for( int i = 0; i < this->rows - 1; ++i)
{
for(int j = 0; j < right.cols - 1; ++j)
{
sum_elems = 0;
for( int k = 0; k < right.rows - 1; ++k)
{
sum_elems += this->the_matrix[i][k] * right.the_matrix[k][j];
}
c.the_matrix[i][j] = sum_elems;
}
}
return c;
}
// Overloaded assignment Operator
template <typename T>
matrix<T>& matrix<T>::operator = (const matrix& right)
{
this->the_matrix= right.the_matrix;
this->rows = right.rows;
this->cols = right.cols;
this->spath = right.spath;
return *this;
}
// Overloaded << operator
template <typename T>
std::ostream& operator << (std::ostream& output, const matrix<T>& left)
{
// Test screen output to see if correct
std::cout << std::setw(14) << "The Matrix:" << '\n';
for( int a = 0; a < left.rows; ++a)
{
for( int b = 0; b < left.cols; ++b)
{
std::cout << ' ' << left.the_matrix[a][b] << ' ';
}
std::cout << '\n';
}
return output;
}
As I suspected, your copy constructor and assignment operator are in fact not implemented correctly. 我怀疑,你的拷贝构造函数和赋值运算符实际上没有正确实现。 You are simply copying the pointer over.
你只是将指针复制过来。 That means that when you copy one matrix to another, they both share the same data.
这意味着当您将一个矩阵复制到另一个矩阵时,它们都共享相同的数据。 When one of them goes out of scope, the destructor is called, then the shared data is deleted, leaving the remaining matrix with dangling pointers.
当其中一个超出范围时,将调用析构函数,然后删除共享数据,使剩余的矩阵具有悬空指针。
Fix those functions so they actually allocate new arrays, and copy the data. 修复这些功能,以便它们实际分配新数组,并复制数据。
i
actually goes from 0
to this->rows - 2
(because of i < n-1
for i = n-1
is false). i
实际上是从0
到this->rows - 2
(因为i < n-1
的i = n-1
是假的)。 Same for other loops. PS If T is type of matrix elements, then type of sum_elems
should be T
. PS如果T是矩阵元素的类型,则
sum_elems
类型应为T
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.