Hello, I am doing a homework assignment and need a bit of help. I have to overload the prefix ++, postfix ++ operator, and the = operator for a circle class. Heres the code I have so far:

#include<iostream>

using namespace std;

class Circle
{
public:
    Circle();
    Circle(int n);
    Circle( const Circle &rhs);
    ~Circle();
    
    Circle operator=(const Circle &rhs);
    Circle operator++();
    Circle operator++(int n);

    int getRadius() const {return *mRadius;}
    void setRadius(int n) {*mRadius=n;}
private:
    int *mRadius;
};

Circle::Circle()
{
    mRadius=new int(5);
}
Circle::Circle(int n)
{
    mRadius=new int(n);
}

Circle::Circle(const Circle &rhs)
{
    mRadius=new int;
    *mRadius=rhs.getRadius();
}
Circle Circle::operator++()//some type of problem
{
    *mRadius++;
    Circle rhs(*mRadius);
    return rhs;
}
Circle Circle::operator=(const Circle &rhs)
{
    if(this==&rhs)
    return *this;
    *mRadius=rhs.getRadius();
    return rhs;
}
Circle::~Circle()
{
    delete mRadius;
}

int main()
{
    Circle circ1;
    Circle circ2(8);
    circ1=circ2;
    ++circ1;
    cout<<circ1.getRadius()<<"\n";
    return 0;
}

So As my comment points out, the ++ operator is not working. It compiles fine but returns this error:

*** glibc detected *** ./homework: free(): invalid pointer: 0x0804a00c ***
======= Backtrace: =========
/lib/libc.so.6[0xb7d85cd0]
/lib/libc.so.6(__libc_free+0x89)[0xb7d87369]
/usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb7f2ba81]
./day10(__gxx_personality_v0+0x16d)[0x8048719]
./day10(__gxx_personality_v0+0x391)[0x804893d]
/lib/libc.so.6(__libc_start_main+0xd8)[0xb7d377c8]
./day10(__gxx_personality_v0+0x65)[0x8048611]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:03 7405624    /home/potentate/programming/projects/homework
08049000-0804a000 rw-p 00000000 08:03 7405624    /home/potentate/programming/projects/homework
0804a000-0806b000 rw-p 0804a000 00:00 0          [heap]
b7c00000-b7c21000 rw-p b7c00000 00:00 0 
b7c21000-b7d00000 ---p b7c21000 00:00 0 
b7d20000-b7d22000 rw-p b7d20000 00:00 0 
b7d22000-b7e47000 r-xp 00000000 08:03 10485778   /lib/libc-2.5.so
b7e47000-b7e48000 r--p 00125000 08:03 10485778   /lib/libc-2.5.so
b7e48000-b7e4a000 rw-p 00126000 08:03 10485778   /lib/libc-2.5.so
b7e4a000-b7e4d000 rw-p b7e4a000 00:00 0 
b7e4d000-b7e57000 r-xp 00000000 08:03 2919748    /usr/lib/libgcc_s.so.1
b7e57000-b7e58000 rw-p 00009000 08:03 2919748    /usr/lib/libgcc_s.so.1
b7e58000-b7e7b000 r-xp 00000000 08:03 10485797   /lib/libm-2.5.so
b7e7b000-b7e7d000 rw-p 00022000 08:03 10485797   /lib/libm-2.5.so
b7e7d000-b7f56000 r-xp 00000000 08:03 2919759    /usr/lib/libstdc++.so.6.0.8
b7f56000-b7f59000 r--p 000d8000 08:03 2919759    /usr/lib/libstdc++.so.6.0.8
b7f59000-b7f5b000 rw-p 000db000 08:03 2919759    /usr/lib/libstdc++.so.6.0.8
b7f5b000-b7f61000 rw-p b7f5b000 00:00 0 
b7f67000-b7f69000 rw-p b7f67000 00:00 0 
b7f69000-b7f6a000 r-xp b7f69000 00:00 0          [vdso]
b7f6a000-b7f84000 r-xp 00000000 08:03 10485776   /lib/ld-2.5.so
b7f84000-b7f85000 r--p 00019000 08:03 10485776   /lib/ld-2.5.so
b7f85000-b7f86000 rw-p 0001a000 08:03 10485776   /lib/ld-2.5.so
bfdaf000-bfdc4000 rw-p bfdaf000 00:00 0          [stack]
Aborted

What is the problem? Thanks. (note: there is something wrong with the c++ syntax highlight. It puts weird html tags in my code)

Why are you bothering making the radius a pointer? It adds unnecessary complications ... not complications really, but there's no need.

using namespace std;

class Circle
{
private:
    int mRadius;

public:
    Circle();
    Circle(int n);
    Circle(const Circle &rhs);
    ~Circle();
    
    Circle operator = (const Circle &rhs);
    Circle operator++();
    Circle operator++(int);

    int getRadius() const {return mRadius;}
    void setRadius(int n) {mRadius=n;}
};

Circle::Circle()
    : mRadius( 5 )
{}

Circle::Circle(int n)
    : mRadius( n )
{}

Circle::Circle(const Circle &rhs)
    : mRadius( rhs.mRadius )
{}

Circle::~Circle()
{}

Circle Circle::operator++()
{
    mRadius++;
    return *this;
}
Circle Circle::operator++(int)
{
    mRadius++;
    return *this;
}
Circle Circle::operator = (const Circle &rhs)
{
    if( this == &rhs )
        return *this;

    mRadius = rhs.mRadius; // Friendly functions :)
    
    return rhs;
}

int main( void )
{
    Circle circ1;
    cout<< circ1.getRadius() <<"\n";

    Circle circ2(8);

    circ1=circ2;
    cout<< circ1.getRadius() <<"\n";

    ++circ1;
    cout<< circ1.getRadius() <<"\n";

    circ1++;
    cout<< circ1.getRadius() <<"\n";
    
    return 0;
}

Well. This is all your code without your pointer int. That isn't a requirement for your assignment is it? It gets rid of the need for a destructor and the need for new etc. I also threw in some initialisation list stuff and made use of the fact that instances are friends with eachother so you don't need to use the get radius method to access the radius.

Your Code is workng fine. U just change *mRadius++ with (*mRadius)++... I compiled ur Code and Executed it is running fine.

wow thanks! Never would of thought that the ++ operator evaluated first!

This question has already been answered. Start a new discussion instead.