-2

I am trying to add two complex numbers using class but keep getting an error. I am also having trouble with the way functions compare and add are declared does the & means the functions are called by reference and is there any special way to define these functions.

#include <iostream> 
#include <set> 
#include <list> 
#include <cmath>

using namespace std;

class Complex {
    int real, img;
    public:
        void getdata() {
            cin >> real >> img;
        }
    void display() {
        cout << real << "+" << img << "i" << endl;
    }
    Complex & compare(Complex & );
    Complex & add(Complex & );
};

Complex & compare(Complex & c2) {
    Complex t;
    if (real > c2.real) {
        t.real = real; //getting is private within this context error for real here
    } else if (c2.real > real) {
        t.real = c2.real;
    } else if (img > c2.img) {
        t.img = img;
    } else if (c2.img > img) {
        t.img = c2.img;
    }
    return t;
}

Complex & add(Complex & a) {
    Complex temp;
    temp.real = real + a.real;
    temp.img = img + a.img;
    return temp;
}

int main() {
    Complex c1, c2, c3;
    c1.getdata();
    c2.getdata();
    c3 = c1.compare(c2);
    c3.display();
    c3 = c1.add(c2);
    c3.display();
    return 0;
}
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • 1
    Please have a look at this [C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) list. This particular language can't be learned by guessing. – Ron Jan 26 '18 at 13:46
  • `Complex & Complex::compare(Complex& c2)` – 001 Jan 26 '18 at 13:48
  • 1
    For a `class`, the default access modifier is `private` (which means `real` and `int` can't be accessed from outside of a class' member). Now, you declare and implement the **free functions** `add` and and `compare` while you declare but don't implement the **methods** `Complex::add` and `Complex::compare`. – Caninonos Jan 26 '18 at 13:49
  • 1
    Did you even search before asking? There are questions with that exact quote in with accepted answers – UKMonkey Jan 26 '18 at 13:51
  • Thank you Johnny Mopp i figured out the problem. – shashank chaudhary Jan 26 '18 at 13:54
  • 1
    Possible duplicate of ["is private within this context" is being thrown for function which should not be private (GCC 5.3.0, C++11)](https://stackoverflow.com/questions/36168718/is-private-within-this-context-is-being-thrown-for-function-which-should-not-b) – UKMonkey Jan 26 '18 at 13:59

1 Answers1

3
class Complex {
    ...
    Complex& compare(Complex&);
    ...
};

So far, so good: the class Complex has a member function named compare that takes an argument of type Complex&.

Complex& compare(Complex&) {
    ...
}

Also okay, but not what you want. This defines a free function (i.e., not a member function) named compare that takes an argument of type Complex&.

To define the member function you have to mention the name of the class that it's a member of:

Complex& Complex::compare(Complex&) {
    ...
}

This defines the member function compare of the class Complex.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165