程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> 編程-c++ 運算符的重載 運行時出現 debug 請各位大神幫忙找錯

編程-c++ 運算符的重載 運行時出現 debug 請各位大神幫忙找錯

編輯:編程綜合問答
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;

}

最佳回答:


其實是以下這兩個函數沒有滿足重載要求,引起語法的混淆。
int &DoubleScriptedArray::operator()(int s1,int s2 )const
int &DoubleScriptedArray::operator()(int s1,int s2 )

末尾有沒有const,這兩個函數都不是重載

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved