大家好!我用C++写了一个简单的矩阵类,用函数返回矩阵的长度和宽度时,得到长度正确,但有些对象的宽度正确,而另一些对象的宽度却错误,且VS没有报错。因为部分宽度正确,部分却不正确,我真的搞不懂问题出在哪。恳请大家指点,谢谢!
我的代码由三个头文件(模板类)和main组成,同时我也将代码放在了百度网盘,下载地址为:http://pan.baidu.com/s/1eQg8Sgi,谢谢!
dynamicVector.h
#pragma once
#include<stdio.h>
template<class T> class dynamicVector
{
protected:
int dimension; // dimension of the dynamic vector (元素的个数);
T* component; // point to the components of the vector (指针数组);
public:
// dynamicVector( );
dynamicVector( int, const T& );
dynamicVector( const dynamicVector& );
// 符号重载;
const dynamicVector& operator=( const dynamicVector& );
const dynamicVector& operator=( const T& );
const dynamicVector& operator+=( const dynamicVector& );
const dynamicVector& operator-=( const dynamicVector& );
const dynamicVector& operator*=( const T& );
const dynamicVector& operator/=( const T& );
~dynamicVector()
{
delete [] component;
} // destructor
int dim() const
{
return dimension;
} // return the demension;
T& operator() ( int i )
{
return component[i];
} // read/write ith component;
const T& operator[] ( int i ) const
{
return component[i];
} // read only ith component;
};
/*
template<class T>
dynamicVector<T> ::dynamicVector()
{
dimension = 1;
component = new T[dimension];
} // default constructor;
*/
template<class T>
dynamicVector<T> ::dynamicVector( int dim = 1, const T& a = 0 )
: dimension( dim ), component( dim ? new T[dim] : 0 ) // 类成员变量初始化;
{
for (int i = 0; i < dim; i++)
{
component[i] = a;
}
} // constructor;
template<class T>
dynamicVector<T>::dynamicVector( const dynamicVector<T>& v )
: dimension( v.dimension ), component( v.dimension ? new T[v.dimension] : 0 )
{
for (int i = 0; i < v.dimension; i++)
{
component[i] = v.component[i];
}
} // copy constructor;
// 重载‘=’,vector = vector
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator=( const dynamicVector<T>& v )
{
if (this != &v) // 避免赋值给本身;
{
if (dimension > v.dimension) // 当前 vector 的维数大于 v 的维数;
{
delete[]( component + v.dimension ); // 删除多余的空间;
}
if (dimension < v.dimension)
{
delete[] component;
component = new T[v.dimension];
}
for (int i = 0; i < v.dimension; i++) // 当两者维数相同时,对应元素赋值;
{
component[i] = v.component[i];
}
dimension = v.dimension;
}
return *this;
} // assignment operator
// 重载符号‘=’,vector[i] = a
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator=( const T& a )
{
for (int i = 0; i < dimension; i++)
{
component[i] = a;
}
return *this;
} // assignment operator with a scalar argument
// 重载符号‘+=’,vectorA = vectorA + vectorB
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator+=( const dynamicVector<T>& v )
{
for (int i = 0; i < dimension; i++)
{
component[i] += v[i];
}
return *this;
}// adding a dynamicVector to the current one
// 重载‘-=’
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator-=( const dynamicVector<T>&v )
{
for (int i = 0; i < dimension; i++)
{
component[i] -= v[i];
}
return *this;
} // subtract a dynamicVector from the current one
// 重载‘*=’
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator*=( const T& a )
{
for (int i = 0; i < dimension; i++)
{
component[i] *= a;
}
return *this;
} // multiply the current dynamicVector by a scalar
// 重载‘/=’
template<class T>
const dynamicVector<T>&
dynamicVector<T>::operator/=( const T& a )
{
for (int i = 0; i < dimension; i++)
{
component[i] /= a;
}
return *this;
} // pide the current dynamicVector by a scalar
// 重载符号‘+’,vectorC = vectorA + vectorB
template<class T>
const dynamicVector<T>
operator+( const dynamicVector<T>& u, const dynamicVector<T>& v )
{
return dynamicVector<T>( u ) += v;
} // dynamicVector plus dynamicVector
// 重载符号‘-’,vectorA = vectorA * -1.0;
template<class T>
const dynamicVector<T>
operator-( const dynamicVector<T>& u )
{
return dynamicVector<T>( u ) *= -1.0;
} // negative of a dynamicVector
// 重载‘-’
template<class T>
const dynamicVector<T>
operator-( const dynamicVector<T>&u, const dynamicVector<T>&v )
{
return dynamicVector<T>( u ) -= v;
} // dynamicVector minus dynamicVector
// 重载‘*’
template<class T>
const dynamicVector<T>
operator*( const dynamicVector<T>&u, const T& a )
{
return dynamicVector<T>( u ) *= a;
} // dynamicVector times scalar
// 重载‘*’
template<class T>
const dynamicVector<T>
operator*( const T& a, const dynamicVector<T>&u )
{
return dynamicVector<T>( u ) *= a;
} // T times dynamicVector
// 重载‘*’
template<class T>
T operator*( const dynamicVector<T>&u, const dynamicVector<T>&v )
{
T sum = 0;
for (int i = 0; i < u.dim(); i++)
{
sum += u[i] * +v[i];
}
return sum;
} // inner product
// 重载‘/’
template<class T>
const dynamicVector<T>
operator/( const dynamicVector<T>&u, const T& a )
{
return dynamicVector<T>( u ) /= a;
} // dynamicVector pided by scalar
// print vector;
template<class T>
void printOut( const dynamicVector<T>& v )
{
//print( "(" );
std::cout << "( ";
for (int i = 0; i < v.dim(); i++)
{
//printf( "v[%d]=", i );
//print( v[i] );
std::cout << v[i] << " ";
}
//print( ")\n" );
std::cout << " )" << std::endl;
} // printing a dynamicVector
/*
template<class T>
void writeOut( dynamicVector<T>& v, const char& txtName )
{
ofstream writeTxt( txtName, ios::out );//供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式);
writeTxt << "( ";
for (int i = 0; i < l.size(); i++)
{
writeTxt << v[i] << " ";
}
writeTxt << " )" << std::endl;
} // write a list in to txt
*/
list.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
template<class T> class list
{
protected:
int number; // the number of items; list中单元(dynamic vector)的个数
T** item; // "item" to store their addresses. 指针的指针,指向指针数组中的指针(地址);
public:
list( int n = 0 ) :number( n ), item( n ? new T*[n] : 0 )
{
} // constructor
list( int n, const T& t )
: number( n ), item( n ? new T*[n] : 0 )
{
for (int i = 0; i < number; i++)
{
item[i] = new T( t );
}
} // constructor with T argument
list( const list<T>& ); // copy constructor
const list<T>& operator=( const list<T>& ); // 重载‘=’;
~list()
{
for (int i = 0; i < number; i++)
{
delete item[i]; // deletes first the pointers in the array in the "list" object
}
delete[] item; // then the entire array itself:
} // destructor;
// accessing the size of the list (number of items);
const int size()
{
return number;
} // list size
// accessing items by overriding '()' and '[]';
T& operator()( int i )
{
if (item[i])
{
return *( item[i] );
}
} // read/write ith item
const T& operator[]( int i ) const // 'const' means read only;
{
if (item[i])
{
return *( item[i] );
}
} // read only ith item
};
template<class T>
list<T>::list( const list<T>& l ) :number( l.number ), item( l.number ? new T*[l.number] : 0 )
{
for (int i = 0; i < l.number; i++)
{
if (l.item[i])
{
item[i] = new T( *l.item[i] );
}
}
} // copy constructor
template<class T>
const list<T>&
list<T>::operator=( const list<T>& l )
{
if (this != & l) // 避免赋值给自身;
{
// make sure that the current list contains
// the same number of items as the list ’l’ that is passed as an argument
if (number > l.number)
{
delete[]( item + l.number );
}
if (number < l.number)
{
delete[] item;
item = new T*[l.number];
}
for (int i = 0; i < l.number; i++)
{
if (l.item[i])
{
item[i] = new T( *l.item[i] );
}
}
number = l.number;
}
return *this;
} // assignment operator
template<class T>
void printOut( const list<T>& l )
{
for (int i = 0; i < l.size(); i++)
{
//printf( "i=%d:\n", i );
//print( l[i] );
std::cout << "i= " << i << std::endl;
// std::cout << l[i] << std::endl;
printOut(l[i]);
}
} // printing a list
template<class T>
void writeOut( const list<T>& l, char* txtName )
{
std::ofstream writeTxt( txtName, std::ios::app );//供写,文件不存在则创建,若文件已存在则清空原内容(ofstream默认的打开方式);
for (int i = 0; i < l.size(); i++)
{
writeTxt << "i= " << i << std::endl;
writeTxt << l[i] << std::endl;
}
} // write a list in to txt
dynamicMatrix.h
#pragma once
#include <algorithm>
#include "dynamicVector.h"
#include "list.h"
template<class T> // "dynamicMatrix" class 存放系数矩阵;
class dynamicMatrix : public list < dynamicVector<T> > // derived from list < dynamicVector<T> >;
{
public:
dynamicMatrix();
dynamicMatrix( int m, int n, const T& ); // constructor;
// 符号重载;
const dynamicMatrix<T>& operator+=( const dynamicMatrix<T>& );
const dynamicMatrix<T>& operator-=( const dynamicMatrix<T>& );
const dynamicMatrix& operator*=( const T& );
//const dynamicMatrix<T>& operator=( const dynamicMatrix<T>& ); // 重载‘=’;
// 'operators()' allow one to refer to the elements D j,j − 1 ,
// D j,j , and D j,j + 1 simply as "D(j,j-1)", "D(j,j)", and "D(j,j+1)", respectively.
T& operator()( int i, int j )
{
return ( *item[i] )( j );
} // (i,j)th element (read/write)
// the read-only version of 'operator()'
const T& operator()( int i, int j, char* ) const // 'const' for reading only;
{
return ( *item[i] )[j];
} // (i,j)th element (read only)
// 返回vector中元素的个数,列数;
int width() const
{
return item[0]->dim();
} // width of grid
// 返回list中vector的个数,行数;
int lenght() const
{
return number;
}
};
template<class T>
dynamicMatrix<T>::dynamicMatrix()
{
number = 1; // number继承自list基类,此处表示dynamicVector的个数,即item的个数;
item = new dynamicVector<T>*[1]; // 设置dynamicVector中元素的个数;
for (int i = 0; i < number; i++)
{
item[i] = new dynamicVector<T>( 1, T(0.0) );
}
}
// constructor,形成一个(m*n)的矩阵;
template<class T>
dynamicMatrix<T>::dynamicMatrix( int m = 1, int n = 1, const T& a = 0 ) // m为行数, n为列数;
{
number = m; // number继承自list基类,此处表示dynamicVector的个数;
item = new dynamicVector<T>*[n]; // 设置dynamicVector中元素的个数;
for (int i = 0; i < number; i++)
{
item[i] = new dynamicVector<T>( n, a );
}
// std::cout << m << " " << n << std::endl;
} // constructor
// 重载‘+=’
template<class T>
const dynamicMatrix<T>& dynamicMatrix<T>::operator+=( const dynamicMatrix<T>& d )
{
for (int i = 0; i < number; i++)
{
*item[i] += d[i];
}
return *this;
} // adding another "dynamicMatrix" to the current one
// 重载‘-=’
template<class T>
const dynamicMatrix<T>& dynamicMatrix<T>::operator-=( const dynamicMatrix<T>& d )
{
for (int i = 0; i < number; i++)
{
*item[i] -= d[i];
}
return *this;
} // subtracting a "dynamicMatrix" from the current one
// 重载‘*=’
template<class T>
const dynamicMatrix<T>& dynamicMatrix<T>::operator*=( const T& t )
{
for (int i = 0; i < size(); i++)
{
*item[i] *= t;
}
return *this;
} // multiplying the current "dynamicMatrix" by a scalar T
// 重载‘+’
template<class T>
const dynamicMatrix<T> operator+( const dynamicMatrix<T>& d1, const dynamicMatrix<T>& d2 )
{
return dynamicMatrix<T>( d1 ) += d2;
} // adding two "dynamicMatrix" objects
// 重载‘-’
template<class T>
const dynamicMatrix<T> operator-( const dynamicMatrix<T>& d1, const dynamicMatrix<T>& d2 )
{
return dynamicMatrix<T>( d1 ) -= d2;
} // subtracting two "dynamicMatrix" objects
// 重载‘*’
template<class T>
const dynamicMatrix<T> operator*( const T& t, const dynamicMatrix<T>& d )
{
return dynamicMatrix<T>( d ) *= t;
} // scalar times "dynamicMatrix"
// 重载‘*’
template<class T>
const dynamicMatrix<T> operator*( const dynamicMatrix<T>& d, const T& t )
{
return dynamicMatrix<T>( d ) *= t;
} // "dynamicMatrix" times scalar
// 重载‘*’
template<class T>
const dynamicVector<T> operator*( const dynamicMatrix<T>& d, const dynamicVector<T>& v )
{
dynamicVector<T> dv( v.dim(), 0. );
for (int i = 0; i < v.dim(); i++)
{
for (int j = std::max( 0, i - 1 ); j <= std::min( v.dim() - 1, i + 1 ); j++)
{
dv( i ) += d( i, j, "read" )*v[j];
}
}
return dv;
} // "dynamicMatrix" times vector
template<class T>
void printOut( const dynamicMatrix<T>& m )
{
for (int i = 0; i < m.lenght(); i++ )
{
printOut( m[i] );
}
}
main.cpp
#include "dynamicMatrix.h"
int main()
{
const int volumeNumX = 5;
const int volumeNumY = 7;
std::cout << "It's OK-2!" << std::endl;
dynamicMatrix<double> geoCoeMatrix( volumeNumX*volumeNumY, volumeNumX*volumeNumY, 0.0 );
dynamicMatrix<double> geoCoeRHS( volumeNumX*volumeNumY, 1, 0.0 );
dynamicMatrix<double> initRootDens( volumeNumX*volumeNumY, 1, 0.0 );
dynamicMatrix<double> rootDens( volumeNumX*volumeNumY, 1, 0.0 );
//std::cout << volumeNumX << " " << volumeNumY << std::endl;
std::cout << geoCoeMatrix.lenght() << " " << geoCoeMatrix.width() << std::endl;
std::cout << geoCoeRHS.lenght() << " " << geoCoeRHS.width() << std::endl;
std::cout << initRootDens.lenght() << " " << initRootDens.width() << std::endl;
std::cout << rootDens.lenght() << " " << rootDens.width() << std::endl;
std::cin.get();
return 0;
}