Complex Numbers

Sep 29, 2008 at 1:43am
I found this code online and for some reason can't get it to compile.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <cmath>
#include <iostream>
#include <iomanip>

using namespace std;


class complex
{
     private:
                  float real;               // Real Part
            float imag;      //  Imaginary Part


   public:
          complex(float,float);
          complex(complex&);
          complex operator +(complex);
          complex operator -(complex);
          complex operator *(complex);
          complex operator /(complex);
          complex getconjugate();
          complex getreciprocal();
          float getmodulus();
          void setdata(float,float);
          void getdata();
          float getreal();
          float getimaginary();
          bool operator ==(complex);
          void operator =(complex);
          friend ostream& operator <<(ostream &s,complex &c);
};
//                                        CONSTRUCTOR
                  complex::complex(float r=0.0f,float im=0.0f)
            {
                 real=r;
               imag=im;
            }

//                                 COPY CONSTRUCTOR
            complex::complex(complex &c)
            {
                 this->real=c.real;
               this->imag=c.imag;
            }


            void complex::operator =(complex c)
            {
               real=c.real;
               imag=c.imag;
            }


            complex complex::operator +(complex c)
            {
                 complex tmp;
               tmp.real=this->real+c.real;
               tmp.imag=this->imag+c.imag;
               return tmp;
            }

            complex complex::operator -(complex c)
            {
                 complex tmp;
               tmp.real=this->real - c.real;
               tmp.imag=this->imag - c.imag;
               return tmp;
            }

                    complex complex::operator *(complex c)
            {
                 complex tmp;
               tmp.real=(real*c.real)-(imag*c.imag);
               tmp.imag=(real*c.imag)+(imag*c.real);
               return tmp;
            }

            complex complex::operator /(complex c)
            {
                 float div=(c.real*c.real) + (c.imag*c.imag);
               complex tmp;
               tmp.real=(real*c.real)+(imag*c.imag);
               tmp.real/=div;
               tmp.imag=(imag*c.real)-(real*c.imag);
               tmp.imag/=div;
               return tmp;
            }

            complex complex::getconjugate()
            {
                 complex tmp;
               tmp.real=this->real;
               tmp.imag=this->imag * -1;
               return tmp;
            }

            complex complex::getreciprocal()
            {
                 complex t;
               t.real=real;
               t.imag=imag * -1;
               float div;
               div=(real*real)+(imag*imag);
               t.real/=div;
               t.imag/=div;
               return t;
            }

            float complex::getmodulus()
            {
                 float z;
               z=(real*real)+(imag*imag);
               z=sqrt(z);
               return z;
            }

            void complex::setdata(float r,float i)
            {
                 real=r;
               imag=i;
            }

            void complex::getdata()
            {
                 cout<<"Enter Real:";
               cin>>this->real;
               cout<<"Enter Imaginary:";
               cin>>this->imag;

            }

            float complex::getreal()
            {
                 return real;
            }

            float complex::getimaginary()
            {
                 return imag;
            }

            bool complex::operator ==(complex c)
            {
             return (real==c.real)&&(imag==c.imag) ? 1 : 0;
             }

              ostream& operator <<(ostream &s,complex &c)
            {
                 s<<"Real Part = "<<c.real<<endl
                <<"Imaginary Part = "<<c.imag<<endl;
               s<<"z = "<<c.real<<setiosflags(ios::showpos)
                <<c.imag<<"i"<<endl<<resetiosflags(ios::showpos);
                return s;
            }



int main()
{
     complex a(10.0f,-2.f); // Calls Constructor
   cout<<a<<endl;               // Calls the overloaded << operator
   complex b(-2);         // Calls Constructor
   complex c=b;                    // Calls Copy Constructor
   c=a;                                   // calls overloaded = operator
   b.getdata();                    // Calls Getdata()
   c.getdata();
   if(b==c)            // calls overloaded == operator
      cout<<"b == c";
      else
      cout<<"b != c";


   cout<<endl<<c.getmodulus()<<endl; // calls getmodulus function()
   complex d;
   d=a+b;   // Calls overloaded +  LINE 176
   cout<<d<<endl;
   d=a-b;     // Calls overloaded -  LINE 178
   cout<<d<<endl;
   d=a*b;        // calls overloaded *  LINE 180
   cout<<d<<endl;
   d=a/b;        // calls overloaded /    LINE 182
   cout<<d<<endl;

   return 0;
}


I get errors at the bottom of the code at lines 176, 178, 180, and 182.

1
2
3
4
5
6
7
8
9
10
11
12
13
test.cc: In function 'int main()':
test.cc:176: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:176: error:   initializing argument 1 of 'void complex::operator=(complex)'
test.cc:178: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:178: error:   initializing argument 1 of 'void complex::operator=(complex)'
test.cc:180: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:180: error:   initializing argument 1 of 'void complex::operator=(complex)'
test.cc:182: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:182: error:   initializing argument 1 of 'void complex::operator=(complex)'


What do these errors mean? and how can I fix these?

Thanks in advance.
Sep 29, 2008 at 2:18am
Somebody correct me if I'm wrong; I mainly keep with C, mostly for things like this:

1
2
3
test.cc:176: error: no matching function for call to 'complex::complex(complex)'
test.cc:41: note: candidates are: complex::complex(complex&)
test.cc:176: error:   initializing argument 1 of 'void complex::operator=(complex)'


You can see that it has an error with what is produced from the addition, or "argument 1 of ...". If you look it has no default constructor, so it could be the way it declares the new complex within the addition's definition, which is trying to call a default constructor.

http://www.cplusplus.com/doc/tutorial/classes.html

Read at 'Default Constructor'.
Sep 29, 2008 at 4:07am
I can't really help, but if someone could tell me why I was able to compile that exact code without so much as a warning, that'd be cool...
Sep 29, 2008 at 8:33am
code as given compiles fine on MSVC 2008.
Should copy constructors be constant references -which would make line 41 like this:

complex::complex(const complex &c); // copy constructor
Sep 29, 2008 at 1:22pm
I tried compiling this in my gcc, and received the same error as posted.

Tried making a default constructor, and had more errors. Looking back, my first post looks retarded.

Looking closer at the error, though, it appears the copy constructor is being called, but isn't being sent an address of a complex, just a complex.

Maybe it is called implicitly?

Anyway, the copy constructor is always supplied by the compiler unless explicitly overridden, so it does compile when you comment it out. Should work fine, just removing it.
Sep 29, 2008 at 4:11pm
From the look of it, it wants the address, not the copy.
Sep 29, 2008 at 4:43pm
I commented out the copy constructor and still got the same errors.
Sep 29, 2008 at 5:12pm
I loaded on DEV C++ and it gave the errors you described.
I changed the copy constructor to:
1
2
3
public:
          complex(float,float);
          complex(const complex&); // notice the const in the declaration here 


and the definition to match:
1
2
3
4
5
6
          //COPY CONSTRUCTOR
            complex::complex(const complex &c)
            {
                 this->real=c.real;
               this->imag=c.imag;
            }


Warnings and errors dissappeared.
Sep 29, 2008 at 6:33pm
Yes, a constructor is a copy constructor if and only iff it takes exactly one parameter by const reference (and the parameter is of the same type).
Ie, guestgulkan's version is a copy constructor; the one in the original post
is not.

There are many other problems with the code though (none that would prevent compilation, but are more usability concerns):

1. The copy constructor isn't needed; the default one provided will suffice;
2. The assignment operator, besides not returning a const reference and
taking its parameter by value rather than const reference, is also not
needed since the default one will suffice;
3. All of the member functions except setdata, operator=, and the friend
stream function should be const member functions;
4. All of the math operator functions should take their parameters by
const reference;
5. No default constructor is provided.


Topic archived. No new replies allowed.