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 C++ 如何判断设置快捷键来实现隐藏/显示窗口
  • ¥15 关于#材料工程#的问题:有没有具有电子阻挡层和空穴阻挡层的电池仿真silvaco代码例子或者其他器件具有阻挡层例子的silvaco代码(最好还有相关文献)
  • ¥60 基于MATLAB的TAOD算法
  • ¥15 Groimp使用疑问
  • ¥15 MDK–ARM里一直找不到调试器
  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文