oop - C++ Overriding logical fail -


hey guys have problem 1 of methods (overriding operator , ~ ) when try print object, unexpected happens... need help

this whole code

#include"stdafx.h" #include<iostream> using namespace std;  class complex { private:     double re, im;  public:         complex(double _re = 0, double _im = 0) : re(_re), im(_im){} //class constructor        void print() const     {         cout << re << " + " << "i(" << im << ")" << endl;     }     complex operator~() const     {         return (re, -im);     } };   void main() {     complex x(2, 3);     x.print();     (~x).print(); } 

if compile it, i'll correct complex number on screen, when try execute ~ overridden operator displays me - -3 + 0 i.... need help. thanks

sorry posting such brain-dead questions, can't figure out myself....been looking @ damn code more 30 minutes , can't see wrong.

you missing complex(re, -im); in:

complex operator~() const {     return (re, -im); } 

hence return implicit converted complex(-im) (comma operator). might use explicit constructors avoid pitfall this.


Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -