4

So far every operator works fine except for this one. When I run the code, I get the error: "error: postfix 'Complex Complex::operator++(Complex)' must take 'int' as its argument|"

Heres my code:

#include<iostream>
#include<iomanip>
using namespace std;

class Complex
{
    friend istream &operator>>(istream&, Complex&);
    friend ostream &operator<<(ostream&, Complex&);

public:
    Complex(float = 0, float = 0);
    Complex operator+ (Complex);
    Complex operator- (Complex);
    Complex operator* (Complex);

The problem occurs here, tells me that the argument should be an integer, however I'm trying to pass the Class instantiation through like I did with the previous operators.

    Complex operator++ (Complex);
private:
    float real, imag;
};

Complex::Complex(float a, float b)
{
    real = a;
    imag = b;
}

Complex Complex::operator+(Complex one)
{
    Complex two;
    two.real = real + one.real;
    two.imag = imag + one.imag;
    return(two);
}

Complex Complex::operator-(Complex one)
{
    Complex two;
    two.real = real - one.real;
    two.imag = imag - one.imag;
    return(two);
}

Complex Complex::operator*(Complex one)
{
    Complex two;
    two.real = (real * one.real) + (imag * one.imag * (-1));
    two.imag = (real * one.imag) + (one.real * imag);
    return(two);
}

Complex Complex::operator++(Complex one)
{
    Complex two;
    two.real = (real * real * real) + (3 * real) * (imag * imag * (-1));
    two.imag = 3 * (real * real)*imag +  (imag * imag *imag * (-1));
    return(two);
}

//Extraction Operator
istream &operator>> (istream &input, Complex &one)
{
    input >> one.real >> one.imag;
}


//Insertion Operator
ostream &operator<<(ostream &output, Complex &one)
{
    output << one.real <<"+"<< one.imag <<"i" << endl;
    return output;
}

//Write stream insertion and extraction operators

int main()
{
Complex c1,c2,c3,sum,diff,prod;
cout << "Enter first complex number: ";
cin >> c1;
cout <<"Enter second complex number: ";
cin >> c2;
cout << "The first complex number is: " <<c1;
cout <<"The second complex number is: " <<c2;
sum = c1 + c2;
cout<<"The sum is: " <<sum;
diff = c1 - c2;
cout<<"The difference is: " <<diff;
prod = c1*c2;
cout<<"The product is: " <<prod;

if (c1==c2)
    cout <<"Equal";
if (c1!=c2)
    cout <<"Not equal";

//Cube function is the ++ operator
cout << "Preincrement: " <<++c1<<++c2;
cout << "Postincrement: " <<c1++<<c2++;
cout << "After post increment: "<<c1<<c2;
*/
return 0;
}
Luke D
  • 59
  • 6

1 Answers1

4

You need to use (int) or () as a suffix to operator++. This will tell the compiler whether you want the ++ operator to be pre, or postfix. I.e. int++ or ++int.

It's just an idiosyncrasy.

class Point  
{  
public:  
    // Declare prefix and postfix increment operators.  
    Point& operator++();       // Prefix increment operator.  
    Point operator++(int);     // Postfix increment operator.
private:
    int x;
};

// Define prefix increment operator.  
Point& Point::operator++()  
{  
    x++;   
    return *this;  
}  

// Define postfix increment operator.  
Point Point::operator++(int)  
{  
    Point temp = *this;  
    ++*this;  
    return temp;  
} 
pcgben
  • 726
  • 7
  • 24