2

I am trying to write a code to overload the postfix increment function, but it functions like a postfix increment and I don't know how to fix it. Here is my class definition:

#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;
class Array{
    private:
    int * ptr;
    int m_size;
    static int total_size;

    public:
    Array(int);
    Array(Array&);
    friend ostream& operator<<(ostream& os,const Array& obj);
    int& operator[](int);
    int operator*();    
    Array& operator=(Array&);                                                                                                                                                                                                                                                                                                                                                                                                                                           
    Array& operator++(int);
    Array& operator--();
    bool operator==(Array&);    
    bool operator<(Array&);
    Array& operator ! ();
    int& operator * (Array&);
    Array& operator += (Array&);
    static int getNumberOfElements();
    int getSize();
};

Here is the overloaded++ code:

Array& Array::operator++(int){
    for (int x=0; x<getSize(); x++){
        *(ptr+x) += 1;
    }
    return *this;
}   

In main.cpp here is what I got:

/**
Original arr1: 2 1 5 3 4 0 2 6 0 9 7 8 3 1 4 
**/
cout << "arr1++: " << arr1++;
cout << "arr1 is: " << arr1 << endl;
/**
arr1++: 3 2 6 4 5 1 3 7 1 10 8 9 4 2 5 
arr1 is: 3 2 6 4 5 1 3 7 1 10 8 9 4 2 5 
**/

Edit: So I made this:

Array Array::operator++(int){
    Array temp = *this;
    for (int x=0; x<getSize(); x++)
         ptr[x]++;
    return temp;
} 

Program now crashes instead. Here is the crash: undefined reference to `operator<<(std::ostream&, Array const&)

But this has nothing to do with my overloaded <<:

ostream& operator<<(ostream& os,Array& obj){
    for (int x=0; x< obj.getSize();x++){
        os<<obj[x]<<" ";
    }
    os<<endl;
    return os;
}

0 Answers0