Thread: Complex Class help!!!

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Complex Class help!!!

    I'm writing a Class called Complex. and I have overloaded >> and <<.
    but there are still something wrong in the code, please help
    BTW, how do i set failbit to indicate improper input? the input should be the form like : 3 + 8i

    Code:
    #include <iostream>
    
    using namespace std;
    
    class complex{
    
    	friend ostream& operator<<(ostream&, const complex&); //inserter
    
        friend istream& operator>>(istream&, complex&); //extractor
    
    public:
    	complex();
    
    	complex(double,double);
    
    private:
    	double real, imaginary;
    
    }; //end class Complex
    
    complex::complex()  //no parameters, both parts are 0	
    { 
    	real=0.0; 
    	imaginary=0.0; 
    } 
    
    complex::complex(double r, double i = 0.0)  //with parameters
    { 
    	real=r; 
    	imaginary=i; 
    } 
    
    ostream& operator<<(ostream &output, const complex &com )
    {
    	output<<"("<<com.real<<"+"<<com.imaginary<<"i"<<")";
    	return output; //enable ouptput in form of a + bi
    }
    
    istream& operator>>(istream &input, complex &com)
    {
    	input.ignore ();   //skip (
    	input>>com.real;
    	input.ignore (3);  //skip two spaces and +
    	input>>com.imaginary;
    
    	return input;
    
    int main()
    {
    	complex X;
    
    	cout << "Enter a complex number in form of (1.2 + 2.5i)";
    
    	cin>> X;
    
    	cout << "The complex number entered was: ";
    
    	cout<<X<<endl;
    
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Is there any reason you aren't using STL's complex type? Just #include <complex> and use that one. You can google for information on how to use it.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    >>but there are still something wrong in the code, please help<<

    Can you be more specific? Are you getting errors or is it just not acting as you expected, and if not, what is it doing wrong?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    the error is like this "error C2248: 'real' : cannot access private member declared in class 'complex' ". I'm new to C++.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    You might try adding an end bracket to the function above main.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Are you using Microsoft Visual Studio 6.0? If so, there is a documented bug with friend functions that makes it so friend functions can't access private data from a class. Considering thats the only reason you want to use friend functions, it basically makes them worthless. I'm not sure if there is a fix for it, I'd suggest searching Google. In the meantime, when I run into this I usually need to do some stupid hack like so:

    Code:
    #include <iostream>
    
    using namespace std;
    
    class complex{
    
        
    public:
    	complex();
    
    	complex(double,double);
    
         void getComplex();
         void displayComplex();
    
    private:
    	double real, imaginary;
    
    }; //end class Complex
    
    complex::complex()  //no parameters, both parts are 0	
    { 
    	real=0.0; 
    	imaginary=0.0; 
    } 
    
    complex::complex(double r, double i = 0.0)  //with parameters
    { 
    	real=r; 
    	imaginary=i; 
    } 
    
    ostream& operator<<(ostream &output, complex &com )
    {
        com.displayComplex();
    	return output; //enable ouptput in form of a + bi
    }
    
    void complex::displayComplex()
    {
    	cout<<"("<<real<<"+"<<imaginary<<"i"<<")";
    }
    
    istream& operator>>(istream &input, complex &com)
    {
        com.getComplex();
    	return input;
    }
    
    void complex::getComplex()
    {
    	cin.ignore ();   //skip (
    	cin>>real;
    	cin.ignore (3);  //skip two spaces and +
    	cin>>imaginary;
    }
    
    int main()
    {
    	complex X;
    
    	cout << "Enter a complex number in form of (1.2 + 2.5i)";
    
    	cin>> X;
    
    	cout << "The complex number entered was: ";
    
    	cout<<X<<endl;
    
        return 0;
    }

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by PJYelton
    Are you using Microsoft Visual Studio 6.0? If so, there is a documented bug with friend functions that makes it so friend functions can't access private data from a class.
    Really? That's what I use to build this project and it ran fine. The only modification I made was the addition of the end brace of that function.

    I have all the latest service packs, maybe it was addressed in one of the later ones but I don't think so.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yep, adding the brace to his function and compiling gives me the error(s):
    error C2248: 'real' : cannot access private member declared in class 'complex' see declaration of 'real'
    Here is a link to the msdn site concerning the bug:

    http://support.microsoft.com/default...b;en-us;192539

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Microsoft
    A supported fix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Apply it only to computers that are experiencing this specific problem. This fix may receive additional testing. Therefore, if you are not severely affected by this problem, Microsoft recommends that you wait for the next Service Pack 3 for Visual Studio 6.0 that contains this fix.
    So, you do know they have released service pack 6 for Visual C++ 6.0 right?
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #10
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah I noticed that same paragraph too when I pulled up the site. Its been a couple of years since I've needed a friend function (or anything else that is broken) so I never got around to downloading any of the new service packs. Looks like I'll do that now

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    That helps a lot, thanks guys!
    Do u guys know how to do an error checking?
    for example:
    if input (8+9i), it will tell me that is a improper input, and allows me to re-input.
    some kind of clear()? cin.fail()? teach me please! {the proper format should be (8 + 9i) with two blanks in the middle)

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Assuming you have the proper service packs...

    First off, going strictly by what you want as input, you are also going to need to ignore the last two characters 'i' and ')'. After that, you just need to check the stream's state to determine if improper input was entered and if so then prompt and get the input again. The stream will go into an error state under the conditions you have it if (8+9i) is entered and (8 + 9i) is expected.

    Code:
    #include <iostream>
    #include <limits>
    
    using namespace std;
    
    ...
    
    int main()
    {
        complex X;
    
        cout << "Enter a complex number in form of (1.2 + 2.5i): ";
    
        while( !(cin >> X) )
        {
            cout << "Improper input detected, please try again: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
        }
    
        cout << "The complex number entered was: " << X << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    That helps .Thanks a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. complex class imp?
    By stabu in forum C++ Programming
    Replies: 3
    Last Post: 03-12-2009, 04:06 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  4. Problem from texbook
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-26-2002, 04:55 AM