Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to fix the error: ‘int complex_number::n’ is private within this context, in my code? What is wrong?

+5 votes
asked Aug 5 by npatel300 (1,440 points)
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using std::ostream;

struct complex_number{

    complex_number(int n): n(n){}

    complex_number operator+(const complex_number &)const;
    complex_number operator-(const complex_number &)const;
    complex_number operator*(const complex_number &)const;
    complex_number operator/(const complex_number &)const;
    
    complex_number operator+(int) const;
    complex_number operator-(int) const;
    complex_number operator*(int) const;
    complex_number operator/(int) const;
    
    complex_number operator+(double) const;
    complex_number operator-(double) const;
    complex_number operator*(double) const;
    complex_number operator/(double) const;

    friend complex_number operator+(int, const complex_number &);
    friend complex_number operator-(int, const complex_number &);
    friend complex_number operator*(int, const complex_number &);
    friend complex_number operator/(int, const complex_number &);
    
    friend complex_number operator+(double, const complex_number &);
    friend complex_number operator-(double, const complex_number &);
    friend complex_number operator*(double, const complex_number &);
    friend complex_number operator/(double, const complex_number &);

    friend ostream &operator<<(ostream &, const complex_number &);
    
private:
    int n;
};

#endif /* COMPLEX_H */

/* complex.cc */
#include <iostream>

struct complex_number operator+(int n, complex_number &o) {

    return complex_number(o + n);
}

complex_number operator-(int n, complex_number &o) {

    return complex_number(n) - o;
}

complex_number operator*(int n, complex_number &o) {

    return complex_number (o * n);
}

complex_number operator/(int n, complex_number &o) {

    return complex_number(n) / o;
}

ostream &operator<<(ostream &out, complex_number &o) {

    return out<<o.n;
}

/* main.cc */

#include <iostream>

using std::cout;
using std::endl;

int main(void)
{
    int a = 4;
    int b = 3;
    int c = 2;
    int d = 1;
    
    double n = 5.0;
    cout << a + b << " + " << c + d << " = " << complex_number << endl;
    cout << a + b << " - " << c + d << " = " << complex_number << endl;
    cout << a + b << " * " << c + d << " = " << complex_number << endl;
    cout << a + b << " / " << c + d << " = " << complex_number << endl;

    cout << a + b << " + " << 5.0 << " = " << complex_number << endl;
    cout << a + b << " - " << 5.0 << " = " << complex_number << endl;
    cout << a + b << " * " << 5.0 << " = " << complex_number << endl;
    cout << a + b << " / " << 5.0 << " = " << complex_number << endl;

    cout << 5.0 << " + " << a + b << " = " << complex_number << endl;     
    cout << 5.0 << " - " << a + b << " = " << complex_number << endl;
    cout << 5.0 << " * " << a + b << " = " << complex_number << endl;
    cout << 5.0 << " / " << a + b << " = " << complex_number << endl;

    return 0;
}

1 Answer

0 votes
answered Aug 5 by Peter Minarik (32,240 points)

Look at all the warnings. They show the line numbers.

For your specific question:

main.cpp:68:19: error: ‘int complex_number::n’ is private within this context
     return out<<o.n;
                   ^
main.cpp:38:9: note: declared private here
     int n;
         ^

So you're trying to access the field n of structure o (on line 68) However, (on line 38) n is declared to be private. Hence you have the error: you're trying to access a field (variable of an object/struct) that you have no permission to.

You tried to crate a friend function declaration:

friend ostream &operator<<(ostream &, const complex_number &);

, however during the implementation you missed the const from the second parameter, and now the signature of the two functions do not match, and your function is not a friend of the complex_number structure.

To fix it, use the correct signature:

ostream &operator<<(ostream &out, const complex_number &o) {
    return out<<o.n;
}

After this, your code compiles, but does not link as none of the complex_number operators have been defined (only the friend functions, but not the ones that are part of the structure).

Some extra notes:

  • You should keep your files separated. Do not copy everything into one giant file. Use multiple header and source files as the code structure demands.
  • A complex number has two component: a real part and an imaginary part. Your complex_number only has one of them (so it's not a complex number in mathematical sense). Reference:  https://en.wikipedia.org/wiki/Complex_number
commented Aug 6 by npatel300 (1,440 points)
I know complex numbers has real and imaginary part. But, I don't know if I fixed it right, but I think the output produces wrong calculation for complex numbers. why is that so? please help me. Thank you.
https://onlinegdb.com/iXRjD1DZy
commented Aug 6 by Peter Minarik (32,240 points)
Which calculation do you think is wrong? I don't see problem with the results of the calculations.
commented Aug 6 by npatel300 (1,440 points)
The complex number (4 + 4i) + (2 + 1i) should be 6 + 5i, and the result from my output shows 11 as the answer. Am I doing wrong implementation for complex numbers?
commented Aug 7 by Peter Minarik (32,240 points)
I don't see that line of code. However, I think your problem is that you think (4 + 4) means the complex number 4, 4j to the C/C++ compiler. It does not. It means the integral number 4 + 4 = 8.

This is how you use std::complex: https://en.cppreference.com/w/cpp/numeric/complex
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...