I need help fixing errors!!

This code is meant to open a file and use overloaded operators for a complex number class. I am getting a lot of errors in my class declaration/definition but I am not sure why. Please take a look and see if you can spot anything!!

I will list the error messages I am receiving below the code. Thanks!


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
187
188
189
190
191
192
193
194
195
196
197
198
#include <iostream>
#include <cmath>
#include <fstream>

class CN
{
	public: 
		double real;
		double im;
	
		CN();
		CN(double, double);
		CN(double);
		double getReal();
		double getIm();
		void setReal(double);
		void setIm(double);
		bool operator ==(CN z, CN w);
		friend bool areequal(CN z);
		bool CN::operator == (CN z, CN w);
		bool CN::operator < (CN z, CN w);
		friend CN operator * (CN z, CN w);
		friend CN operator + (CN z, CN w);
		friend CN operator();
		friend ostream& operator << (ostream& outs, CN z);
		friend istream& operator >> (istream& ins, CN& z);
};

CN::CN()
{
	real= 0;
	im= 0;
}

CN::CN(double x, double y)
{
	real= x;
	im= y;
}

double CN::getReal()
{
	return real;
}

double CN::getIm()
{
	return im;
}
void CN::setReal(double x)
{
	real= x;
}

void CN::setIm(double y)
{
	im= y;
}

bool CN::areequal(CN z)
{
	return (real==z.real && z.im);
}

bool CN::operator == (CN z, CN w)
{
	return z.real==w.real && z.im==w.im;
}

bool CN::operator < (CN z, CN w)
{
     return sqrt((z.real*z.real) + (z.im*z.im)) > sqrt((w.real*w.real) + (w.im *w.im));
}

CN operator * (CN z, CN w)
{
       CN product;
       product.real = (z.real*w.real) - (z.im*w.im)
       product.im = (z.real*w.im)+ (w.real*z.im);
       
}

CN operator + (CN z, CN w)
{
       CN sum;
       sum.real = (z.real + w.real);
       sum.im = (z.im + w.im);
}

CN operator - ()
{
	CN temp;
	temp.real= -real;
	temp.im= -im;
	return temp;
}

ostream& operator <<(ostream& outs, CN& z)
{
    if (z.im < 0.0)
    {
       outs << z.real << z.im; 
    }
    else if (z.im == 1)
    {
       outs << z.real << " + i";
    else if (z.im == -1)
    {
       outs << z.real << " - i";
    }
    else if (z.real == 0.0)
    {
       outs << z.im << "i";
    }
    else if (z.im == 0.0)
    {
       outs << z.real;
    }
    else
	{
    outs << z.real << " + " << z.im << "i";
    }
	return outs;
}

istream& operator >> (istream& ins, CN& z)
{
	ins << z.real << z.im;
	return ins;
}



using namespace std;
int main()
{
    CN array[10];
	istream ins;
	ostream outs;
	ins.open("P:\Class\Math\Aydin\118\FilesForLabs\inputdata.txt");
	outs.open("outputdata.txt");
	
	for (int i=0; i<9; i++)
	    {
        ins >> array[i];
        cout << array[i];
        outs << array[i];
        }
     cout << "Please enter one more complex number.\n";
     cin >> array[9];
     outs << array[9];
     
     for (int k=0; k<10; k++)
     {
      cout << array[k];
     }
     
	
	sort(array);

return 0;

};
	
void sort (CN array[10])
{
	int index_of_next;
	for (int index=0; index < 10; index++)
	{
		index_of_next = index_of_smallest(array, index);
		swap_values(array, index, index_of_next);
	}
}
	
void swap_values(CN array[10], int i, int j)
{
	CN temp= array[i];
	temp = array[i];
	array[i] = array[j];
	array[j]= temp;
}


int index_of_smallest(CN array[10], int index)
{
	CN min = array[index];
	int index_of_min = index;
	
	for (int k = index + 1; k <10; k++)
	{
		if(array[k] < min)
		{
			min = array[k];
			index_of_min = k;
		}
	}
	return index_of_min;
}



ERROR MESSAGES:

Lab_ComplexNumbers.cpp:46:8: error: overloaded 'operator==' must be a binary operator (has 3 parameters)
bool operator ==(CN z, CN w);
^
Lab_ComplexNumbers.cpp:48:12: error: extra qualification on member 'operator=='
bool CN::operator == (CN z, CN w);
~~~~^
Lab_ComplexNumbers.cpp:49:12: error: extra qualification on member 'operator<'
bool CN::operator < (CN z, CN w);
~~~~^
Lab_ComplexNumbers.cpp:49:12: error: overloaded 'operator<' must be a binary operator (has 3 parameters)
Lab_ComplexNumbers.cpp:52:13: error: friends can only be classes or functions
friend CN operator();
^
Lab_ComplexNumbers.cpp:53:10: error: unknown type name 'ostream'; did you mean 'std::ostream'?
friend ostream& operator << (ostream& outs, CN z);
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:53:32: error: unknown type name 'ostream'; did you mean 'std::ostream'?
friend ostream& operator << (ostream& outs, CN z);
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:54:10: error: unknown type name 'istream'; did you mean 'std::istream'?
friend istream& operator >> (istream& ins, CN& z);
^~~~~~~
std::istream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:146:38: note: 'std::istream' declared here
typedef basic_istream<char> istream;
^
Lab_ComplexNumbers.cpp:54:32: error: unknown type name 'istream'; did you mean 'std::istream'?
friend istream& operator >> (istream& ins, CN& z);
^~~~~~~
std::istream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:146:38: note: 'std::istream' declared here
typedef basic_istream<char> istream;
^
Lab_ComplexNumbers.cpp:88:10: error: out-of-line definition of 'areequal' does not match any declaration in 'CN'
bool CN::areequal(CN z)
^~~~~~~~
Lab_ComplexNumbers.cpp:106:52: error: expected ';' after expression
product.real = (z.real*w.real) - (z.im*w.im)
^
;
Lab_ComplexNumbers.cpp:118:4: error: overloaded 'operator-' must have at least one parameter of class or enumeration type
CN operator - ()
^
Lab_ComplexNumbers.cpp:121:14: error: use of undeclared identifier 'real'; did you mean 'CN::real'?
temp.real= -real;
^~~~
CN::real
Lab_ComplexNumbers.cpp:36:10: note: 'CN::real' declared here
double real;
^
Lab_ComplexNumbers.cpp:121:14: error: invalid use of non-static data member 'real'
temp.real= -real;
^~~~
Lab_ComplexNumbers.cpp:122:12: error: use of undeclared identifier 'im'; did you mean 'CN::im'?
temp.im= -im;
^~
CN::im
Lab_ComplexNumbers.cpp:37:10: note: 'CN::im' declared here
double im;
^
Lab_ComplexNumbers.cpp:122:12: error: invalid use of non-static data member 'im'
temp.im= -im;
^~
Lab_ComplexNumbers.cpp:126:1: error: unknown type name 'ostream'; did you mean 'std::ostream'?
ostream& operator <<(ostream& outs, CN& z)
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:126:22: error: unknown type name 'ostream'; did you mean 'std::ostream'?
ostream& operator <<(ostream& outs, CN& z)
^~~~~~~
std::ostream
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iosfwd:147:38: note: 'std::ostream' declared here
typedef basic_ostream<char> ostream;
^
Lab_ComplexNumbers.cpp:135:5: error: expected expression
else if (z.im == -1)
^
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

Please copy and paste the exact error message and indicate which line it is referring to.
Sorry about that! I fixed it I believe. There are a lot of error messages at the moment but if someone could help me on a few i think i would have an easier time fixing the rest.
When you define a binary operator as a member of the class, the left hand side is the this pointer, so you should only specify one parameter for the right hand side. If you want to use two parameters, you need to define it as a free function, not as a member of the class.
Isn't the overloaded operators in friend functions, which are not members of a class? Which line(s) are you referring to?
I am referring to the same lines as your compiler. The error messages are explicit: you have too many arguments for a binary operator.
Sorry but I am not entirely sure how I would do that. Don't I need both arguments for my function definition to run properly?
Tell me which part confuses you, and I will elaborate:
LB wrote:
When you define a binary operator as a member of the class, the left hand side is the this pointer, so you should only specify one parameter for the right hand side.
I don't understand what a this pointer is and I also don't know how I would only specify one parameter if my definition uses two.
Topic archived. No new replies allowed.