运算符重载Array类

use*_*433 3 c++ overloading

我正在尝试为Array类重载运算符<< >>!= == =和[].应用程序在运行时崩溃,但没有显示编译错误.什么可能是错的?IDE使用了dev c ++

这是array.h

#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
using namespace std;

class Array{
  friend ostream & operator << ( ostream &, const Array & );
  friend istream & operator >> ( istream &, Array &);
  private:
         int size;
         int * ptr;
  public:
         Array ( int = 10 );
         Array ( const Array & ); //copy constructor
         ~Array ();
         const Array &operator=( const Array & ); 
         bool operator == ( const Array & ) const; 
         bool operator != ( const Array & ) const;
         const int operator [] (int) const; 
         int getSize() const;            
};

#endif
Run Code Online (Sandbox Code Playgroud)

现在是array.cpp

#include <iostream>
using namespace std;
#include "array.h"

Array::Array (int sze ){ //default constructor edited
         size = (sze > 0 ? sze : 10);
         ptr = new int [ size ];
         for (int i = 0;  i < size; i++)
             ptr[ i ] = 0; //initial values
}
Array::Array (const Array & arr ): size(arr.size){
         ptr = new int [size];
         for ( int i = 0; i< size; i++)
             ptr [ i ] = arr.ptr [ i ];
}
Array::~Array(){
         delete [] ptr;
}
const Array &Array :: operator= ( const Array & right){//IMPO
         if(&right != this){ //edited self assignment test
                   if(size != right.size){//diff sized arrays
                           delete [] ptr; //reclaim space
                           size = right.size; 
                           ptr = new int [ size ]; //space created
                   }
         }
         for(int i=0; i<size; i++)
                 ptr[ i ] = right.ptr[ i ];
         return *this;     //enables cascading a=b=c       
}
bool Array::operator == ( const Array & right) const{
         if ( size != right.size )
            return false;
         for ( int i =0; i < size; i++ ){
             if ( ptr [ i ] != right.ptr[ i ] )
                return false;
         }
         return true;
 }
bool Array::operator != ( const Array & right ) const{ //edited
         return ! (*this == right);
}
const int Array::operator [] (int subscript) const{
         if(subscript >=0 && subscript < size)
            return ptr[ subscript ];      
}
int Array::getSize() const{ return size; }  
//friend functions not in .h
ostream & operator << ( ostream & output, const Array & array){
         for (int i = 0; i < array.size; i++)
             output << array.ptr[i] ; 
}
istream & operator >> ( istream & input, Array & array){
         for (int i = 0; i < array.size; i++)
             input >> array.ptr[i];
}
Run Code Online (Sandbox Code Playgroud)

现在main.cpp

#include <cstdlib>
#include <iostream>
#include "array.h" // " " not <>
using namespace std;

int main(int argc, char *argv[])
{
Array a1(7),a2 (-1),a4; //changed a2
cout<<"Input "<<a1.getSize()<<" integers for Array object a1 and "<<a2.getSize()<<" integers for Array objecta2\n";
cin>>a1>>a2;
cout<<"a1 and a2 are\n";
cout<<a1<<endl<<a2;
cout<<"a1!=a2 : "<<(a1!=a2)<<endl;
cout<<"a1 ==a2: "<<(a1==a2)<<endl;
cout<<"Printing a1[5] : "<<a1[5]<<endl;
Array a3(a1); 
a4 = a3;

system("PAUSE");
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*lex 6

您必须ptr在构造函数中保留内存.

Array::Array (int size ){ //default constructor
         size = (size > 0 ? size : 10);
         ptr = new int [size]; // ADD THIS LINE
         for (int i = 0;  i < size; i++)
             ptr[ i ] = 0; //initial values
}
Run Code Online (Sandbox Code Playgroud)

您的代码还存在一些其他问题,这些问题不是崩溃的直接来源,但值得注意:

  1. Array::operator !=是根据自身定义的.它应该类似于operator==,或者您可以重复使用它

    if( *this == right )
        return false;
    return true;
    
    Run Code Online (Sandbox Code Playgroud)
  2. Array::operator []如果索引超出范围,应该抛出异常.目前它只返回垃圾内存.

  3. Array::Array (int size )赋值中size指定参数,而不是成员.将第一行更改为:

     this->size = (size > 0 ? size : 10);
    
    Run Code Online (Sandbox Code Playgroud)
  4. operator<<并且operator>>应该返回outputinput分别.

    ostream & operator << ( ostream & output, const Array & array){
       for (int i = 0; i < array.size; i++)
           output << array.ptr[i] ; 
       return output;
    }
    
    Run Code Online (Sandbox Code Playgroud)