455,433 Members | 1,844 Online Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

# friend ostream& operator <<, compiles and runs in Dev++ no output or error in Borland

4 Replies

 P: n/a There are many issues with your code. Why does your Complex class have so many members and things irrelevant to complex numbers? (By the way did you know the standard already has a complex?) One clear error in your code is here: Rock wrote: Complex operator +(const Complex& left, const Complex& right) { return(left + right); } that is simply recursing ad-infinitum. Nov 22 '05 #2

 P: n/a It's a non-finished homework assignment, written by a 4 week old (in C++ that is). When completed, the user will enter two imaginary numbers in the fomat a+bi, and the program will add, subtract, multiply and check for eqality of the two inputs. Anyway, my question/problem is with the member function: friend ostream& operator <<(ostream& outs, const Complex& number); taking me to this output line: ostream& operator <<(ostream& outs, const Complex& number) { outs << "\n" << "Numbers started with: " << number.l_real << number.l_sign << abs(number.l_imaginary) << "i" << " and " << number.r_real << number.r_sign << abs(number.r_imaginary) << "i" << "." << "\n" << "\n" << "Added they equal: " << number.real_result1 << "+" << number.imaginary_result1 << "i" << endl << "Subtracted they equal: " << number.real_result2 << "+" << number.real_result2 << "i" << endl << "Multiplied they equal: " << number.real_result3 << "+" << number.imaginary_result3 << "i" << endl << "They are " << number.answer << "." << "\n" << endl; return outs; This menagerie compiles and runs on DevC++, however, when compiled on Borland 5.5, it compiles fine, but when run, it takes the two inputs and promptly (no puns intended) returns to the command prompt with no errors and no output....nothing.... Nov 22 '05 #3

 P: n/a Hi Rock, I suggest you cut your code back to something basic like: #include using namespace std; class Complex { public: Complex(long& r, long& i):real(r), imaginary(i) {}; friend ostream& operator << (ostream& outs, const Complex& number); private: long real; long imaginary; }; ostream& operator << (ostream& outs, const Complex& number) { outs << number.real << " + " << number.imaginary << "i"; return outs; } int main () { long real, imaginary; cout << endl << "Enter a complex number in the form of a + bi: " << endl; cout << "Enter real part" << endl; cin >> real; cout << "Enter imaginary part" << endl; cin >> imaginary; Complex complex(real, imaginary); cout << complex << endl; return 0; } Once you got that working in both environments start studying overloading operators, and start adding addition, subtraction, etcetera. Good luck. Nov 22 '05 #4

 P: n/a "Rock" wrote in message news:11**********************@g14g2000cwa.googlegr oups.com... I'm in the process of writing this program for complex numbers and I use DevC++. My professor on the other hand compiles on Borland 5.5. So I ocasionally save and run my work on Borland to see if it caught anything, it's very picky... Anyway, the code below works on Dev, and it compiles fine on Borland, but when I run it from borland, there is no output, no error, it just skips right over the freind ostream call. HELP! I'm new to this and so far it's been a very trying experience... any help would be greatly appreciated. Rick .... //Class for complex numbers. class Complex { public: .... friend ostream& operator <<(ostream& outs, const Complex& number); .... }; int main() { .... Complex start(answer1, answer2); cout << "\n" << start; .... } .... ostream& operator <<(ostream& outs, const Complex& number) { outs << "\n" << "Numbers started with: " << number.l_real << number.l_sign << abs(number.l_imaginary) << "i" << " and " << number.r_real << number.r_sign << abs(number.r_imaginary) << "i" << "." << "\n" << "\n" << "Added they equal: " << number.real_result1 << "+" << number.imaginary_result1 << "i" << endl << "Subtracted they equal: " << number.real_result2 << "+" << number.real_result2 << "i" << endl << "Multiplied they equal: " << number.real_result3 << "+" << number.imaginary_result3 << "i" << endl << "They are " << number.answer << "." << "\n" << endl; return outs; } It seems to me that the line: cout << "\n" << start; is simply not seeing the overloaded << operator. Why this would be, I'm not enough of a C++ guru to figure out. One thing I would try would be tomove the overloaded << definition above main just for the heck of it to see if that did anything, because you are using the overload before it is defined publicly. Not sure if that would do anything, but I would try it anyway. Nov 22 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion. 