Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8756793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:09:26+00:00 2026-06-13T14:09:26+00:00

i am trying to overload operators << >> != == = and [] for

  • 0

i am trying to overload operators << >> != == = and [] for Array class.
The app crashes on run, though no compilation errors are shown.
what could possibly be wrong? IDE used dev c++

Here’s 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

and now 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];
}

now 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:09:27+00:00Added an answer on June 13, 2026 at 2:09 pm

    You have to reserve memory for ptr in the constructor.

    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
    }
    

    There are some other problems with your code that are not the direct source of the crash but are worth noting:

    1. Array::operator != is defined in terms of itself. It should be similar to operator==, or you can re-use it with

      if( *this == right )
          return false;
      return true;
      
    2. Array::operator [] should probably throw an exception if the index is out of bounds. Currently it just returns garbage memory.

    3. Inside Array::Array (int size ) the assignment to size assigns to the parameter, not to the member. Change the first line to:

       this->size = (size > 0 ? size : 10);
      
    4. operator<< and operator>> should return output and input, respectively.

      ostream & operator << ( ostream & output, const Array & array){
         for (int i = 0; i < array.size; i++)
             output << array.ptr[i] ; 
         return output;
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to overload operators of a C++ class using Boost.Python. According to
I am trying to overload my operators its really just a class that holds
Trying to overload the < and > operators in a c++ class template <typename
I am trying to overload the subscript operator ([]) on an abstract class, the
I'm trying to overload the '--' postfix operator. I have this code: class Counter
Hey, I'm trying to figure out if it's possible to overload a template class
I am trying to overload some operators: /* Typedef is required for operators */
I am trying to overload the += operator for my rational number class, but
I've got another problem when trying to overload the () operator for array access:
I'm trying to overload operator<< in the standard way. I have a class called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.