guihesu 2015-05-07 01:37 采纳率: 57.1%
浏览 1794
已采纳

c++ 运算符的重载 运行时出现 debug 请各位大神帮忙找错

// Chapter 8 of C++ How to Program
// doubleScriptedArray.h
#ifndef DARRAY_H
#define DARRAY_H

#include

using std::ostream;
using std::istream;

class DoubleScriptedArray {
friend ostream &operator<<(ostream&, const DoubleScriptedArray & );
/* write declaration for overloaded ostream operator */
friend istream &operator>>( istream &,const DoubleScriptedArray & );
public:
DoubleScriptedArray( int = 10, int = 10 );

DoubleScriptedArray( const DoubleScriptedArray & );

~DoubleScriptedArray();

/* write prototype for overloaded = operator /

const DoubleScriptedArray &operator=( const DoubleScriptedArray &);
bool operator==( const DoubleScriptedArray & ) const;
DoubleScriptedArray operator!=(DoubleScriptedArray &right)const
/
write header for operator != */

  { return ! ( *this == right ); }

int &operator()( int, int );
int &operator()(int ,int )const;// lvalue
/* write prototype for overloaded () operator used as
an rvalue */

private:
int rows; // number of rows in array
int columns; // number of columns in array
int *ptr; // pointer to first element of array
};

#endif
#include

using std::cout;
using std::cin;
using std::endl;

#include

using std::setw;

#include
#include
#include "doubleScriptedArray.h"

DoubleScriptedArray::DoubleScriptedArray( int r, int c )
{
rows = ( r > 0 ? r : 10 );
columns = ( c > 0 ? c : 10 );
ptr = new int[ rows * columns ];
assert( ptr != 0 );

for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = 0;

}

DoubleScriptedArray::DoubleScriptedArray(
const DoubleScriptedArray &init )
{
rows = init.rows;
columns = init.columns;

ptr = new int[ rows * columns ];
assert( ptr != 0 );

for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = init.ptr[ i ];

}

/* write definition for destructor */
DoubleScriptedArray::~DoubleScriptedArray()
{
delete []ptr;

}

/* write definition for operator = */
const DoubleScriptedArray &DoubleScriptedArray::operator =(const DoubleScriptedArray &init)
{

 rows = init.rows;

columns = init.columns;

ptr = new int[ rows * columns ];
assert( ptr != 0 );

for ( int i = 0; i < rows * columns; i++ )
ptr[ i ] = init.ptr[ i ];
return *this;

}

bool DoubleScriptedArray::operator==(
const DoubleScriptedArray &right ) const
{
if ( rows != right.rows )
return false;

if ( columns != right.columns )
return false;

for ( int i = 0; i < rows * columns; i++ )
if ( ptr[ i ] != right.ptr[ i ] )
return false;

return true;

}

// Overloaded subscript operator for non-const Arrays
// reference return creates an lvalue
int &DoubleScriptedArray::operator()( int s1, int s2 )
{
assert( s1 > 0 && s1 < rows );
assert( s2 > 0 && s2 < columns );

return ptr[ columns * s1 + s2 ];
}

// Overloaded subscript operator for const Arrays
// const reference return creates an rvalue
/* write overloaded subscript operator that returns an rvalue */
int &DoubleScriptedArray::operator()(int s1,int s2 )const
{
assert( s1 > 0 && s1 < rows );
assert( s2 > 0 && s2 < columns );

return ptr[ columns * s1 + s2 ];

}

istream &operator>>( istream &input, const DoubleScriptedArray &a )
{
for ( int i = 0; i < a.rows * a.columns; i++ )
input >> a.ptr[ i ];

return input;
}

/* write function header for overloaded insertion operator */
ostream &operator<<(ostream & output,const DoubleScriptedArray &a )
{
for ( int i = 0; i < a.rows * a.columns; i++ )
{
output << setw( 6 ) << a.ptr[ i ];

  if ( ( i + 1 ) % a.columns == 0 )
     output << endl;

}

if ( i % a.columns != 0 )
output << endl;

return output;
}
// Chapter 8 of C++ How to Program
// Driver for class DoubleScriptedArray
#include

using std::cout;
using std::cin;
using std::endl;

#include
#include
#include
using namespace std;

#include "doubleScriptedArray.h"

int main()
{
// seed rand function
srand( time( 0 ) );

// create two arrays with different dimensions
DoubleScriptedArray a( 6, 7 ), b( 8, 2 );

cout << "Uninitialized array \"a\" is: \n" << a
<< "Uninitialized array \"b\" is: \n" << b;

// initialize array "a" with random values (0-100)
for ( int i = 0; i < 6; i++ )

  for ( int j = 0; j < 7; j++ )

     a(i,j)=rand()%100;

     /* write statement to insert random elements (reduced 
         to a range of 0 - 100) into the array via 
         the overloaded () */

// use overloaded operator=
b = a;

cout << "\nInitialized array \"a\" is now:\n" << a
<< "Assigning b = a:\n" << b;

// check if arrays are equal using overloaded ==
if ( a == b )
cout << "\"a\" was found to be equal to \"b\"\n";
else
cout << "\"a\" was found to be not equal to \"b\"\n";

// retrieve an array element using overloaded operator()
cout << "The element (2, 1) of array \"a\" is: "
<< a( 2, 1 ) << endl;

// change an element of the array using overloaded operator()
a( 2, 1 ) = -1;
cout << "Changed element (2, 1) to -1: \n" << a;

// check if arrays are still equal
if (a==b /* write condition to check if arrays are equal */ )
cout << "\"a\" was found to be equal to \"b\"\n";
else
cout << "\"a\" was found to be NOT equal to \"b\"\n";

return 0;

}

  • 写回答

4条回答 默认 最新

  • frank_20080215 2015-05-08 23:00
    关注

    其实是以下这两个函数没有满足重载要求,引起语法的混淆。
    int &DoubleScriptedArray::operator()(int s1,int s2 )const
    int &DoubleScriptedArray::operator()(int s1,int s2 )

    末尾有没有const,这两个函数都不是重载

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛