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 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。