General C++ Problem!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
struct complex_number_packet
{
    double real_part;
    double complex_part;
};

class complex_number
{
    public:
        friend ostream& operator<<(ostream&, const complex_number&);
        friend istream& operator<<(istream&, const complex_number&);
    private:
        complex_number_packet input;
};

ostream& operator<<(ostream &user_stream_out ,complex_number &print_complex_number)
{
    print_complex_number.input.real_part = input.real_part;
    print_complex_number.input.complex_part =input.complex_part;
    user_stream_out<<"("<<print_complex_number.input.real_part<<") + ("<<
        print_complex_number.input.complex_part<<")j";
    return user_stream_out;
}
istream& operator>>(istream &user_stream_in ,complex_number &input_complex_number)
{
    user_stream_in>>input_complex_number.input.real_part>>
       input_complex_number.input.complex_part;
    return user_stream_in;
}


but the VS 2013 is saying 'input' in istream & ostream functions as 'Unidentifier'. Although i have made them FRIENDS of class 'complex_number'. . . Help!
I don't get what's the purpose of line 18 and 19. On the LHS of the assignment operator you use input, but what input exactly? I guess you mean some input belonging to a complex_number object but which one? The compiler doesn't know.

I don't think you need those lines at all. operator<< normally don't manipulate the second operand so normally you would make it a const reference.
ostream& operator<<(ostream &user_stream_out , const complex_number &print_complex_number)
What i thought it should be:
Lines 18,19 uses a complex_number object named 'print_complex_number'
and my class has some other functions that do some particular task and result is stored in the class private member function input which is of type struct complex_number_packet.
I wanted to overload the stream insertion and stream extraction operator such that it work for my this class accurately. Therefore, i tried to COPY data of class private member function input and assigned them to class's another object named 'print_complex_number' member by member. Also i declared ostream & istream functions to be friends to class but input which is a private member function isn't declared in this scope. Why it is so? Friends can access private members too.
I think you might confuse friend functions with member functions. A friend function is not a member function of the class. Inside a friend function you can access the private members of the class but you still need an object to access them. That is why you can't access input without specifying the object.
Another way to say this is that your operators are in global scope, so input.real_part and input.complex_part on lines 18 and 19 would have to come from a global variable named input. However, you haven't shown us such a declaration… In fact, your declaration of input is inside class complex_number. Thus, I wonder if you meant to make the output operator a member of that class???
oh i got it. thanks to all i needed an object to acess the input data member. thanks to all once again!
1
2
3
4
5
6
7
8
9
10
11
12
struct complex_number_packet
{
    double real_part;
    double complex_part;
};

class complex_number
{
...
    private:
        complex_number_packet input;
}


Out of topic (just for suggestion):

Why do you want to have real_part, complex_part as part of complex_number_packet and inturn use that struct within complex_number class instead of placing those two element declarations directly within class complex_number as private data members? Are you using struct complex_number_packet for someother purpose outside the scope/meaning of complex_number?
Topic archived. No new replies allowed.