Line 9: remove. With both a default constructor and a constructor where all arguments have defaults, it's ambiguous which one should be called.
Line 10: public should be followed by a colon, not a semicolon.
Line 13: ); should be ):
Line 36: You need a semicolon after all declarations.
Getr() and Geti(): These should be returning the real and imaginary parts of the class. Check what those values are named.
Lines 88-97. It's hard to know how to define the function without knowing what it's supposed to do. It's declared like a default constructor. Lines 91&92 look like they are trying to compute the dot function. Line 94 looks like it's computing the absolute value of the complex number. I suggest that you comment out this entire function. The only reason not to delete it is because you might want to use some of the code later.
Ideally, the Print() function would be replaced by a << operator, but let's leave that for now. You should change it to be a member function.
Okay, those are enough compiler errors for now. The basic problem here is that you wrote a lot of code before testing any of it. Here is a copy with most of the functionality and main program
#if
'ed out. Get this working. Then move the #if 0 in main() down past the next section. Get the newly exposed section working and repeat the process until everything works. Keep in mind that at each step the any problems you have are NOT in the section that is #if'ed out.
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 199 200 201 202 203 204 205 206
|
// Header File Starts here
class Complex
{
private:
int real;
int imag;
// aditional func here length/absolute value functuion...
public:
//Constructors
Complex(int r = 0, int i = 0):
real(r), imag(i)
{
};
//Destructor
~Complex();
//Operators
Complex operator +(Complex & c);
Complex operator +=(Complex & c);
Complex operator -=(Complex & c);
Complex operator /(Complex & c);
Complex operator *(Complex & c);
//Setter
void Setr(int r);
void seti(int i);
//Getter
int Getr()
{
return real;
}
int Geti()
{
return imag;
}
//Member Func ?? Not sure if necessary
void Print();
int length();
};
//Source Code
// #include "HW4Header.h"
#include<iostream>
#include<cmath>
using namespace std;
void
Complex::Print()
{
return cout << "Real Part is r" << r << "Imaginary Part is i" << i << endl;
// What does th print function really do??
}
/*Complex::Complex(int r, int i)
{ no need to define, already defined...
real=r; imag=i;
}*/
Complex::~Complex()
{
//Destructor
}
//Main File starts here
#if 0
Complex
Complex::operator +(Complex & c) // plus operator
{
Complex comp;
comp.real = this->real + c.real;
comp.imag = this->imag + c.imag;
return comp;
}
/*Complex Complex operator += (Complex &c) //minus operator
{
Complex comp;
comp.real = this->real - c.real;
comp.imag = this->imag - c.imag;
return comp;
}*/
Complex
Complex::operator +=(Complex & c) //Add aand assign operator
{
Complex comp;
comp.real = this->real += c.real;
comp.imag = this->imag += c.imag;
return comp;
}
Complex
Complex::operator -=(Complex & c) //subtract and assign operator
{
Complex comp;
comp.real = this->real -= c.real;
comp.imag = this->imag -= c.imag;
return comp;
}
Complex
Complex::operator /(Complex & c) //division operator
{
Complex comp;
comp.real = this->real / c.real;
comp.imag = this->imag / c.imag;
return comp;
}
Complex
Complex::operator *(Complex & c) //multiplication operator
{
Complex comp;
comp.real = this->real * c.real;
comp.imag = this->imag * c.imag;
return comp;
}
double
Complex() // How do I define the function properly here ??
{
Complex comp;
comp.real = this->real * c.real;
comp.imag = this->imag * c.imag;
return (sgrt(pow(r, 2) + pow(i, 2))); //not sure if the variables are defined
//accurately here, can I have two return statements here
//return comp;
}
#endif
using namespace std;
int
main()
{
Complex a(1, 3); // assume (realpart, imaginary)
Complex b(2, 4);
Complex c;
Complex d;
Complex e;
Complex f;
Complex g;
cout << "a is ";
a.Print();
cout << "b is ";
b.Print();
cout << "c is ";
c.Print();
#if 0
cout << "Setting first complex number " << endl;
a.setr();
cout << "Setting second complex number " << endl;
b.seti();
/* Adding two complex numbers */
cout << "Addition of a and b : " << endl;
c = a + b;
c.printComplex();
/* Subtracting two complex numbers */
cout << "Subtraction of a and b : " << endl;
d = a - b;
d.printComplex();
/* Add and assign two complex numbers */
cout << "Add and Assign a and b : " << endl;
d = a += (b);
d.printComplex();
/* Subtract aand assign two complex numbers */
cout << "Subtraction and Assign of a and b : " << endl;
e = a -= (b);
e.printComplex();
/* Multiply two complex numbers */
cout << "Multiplication of a and b : " << endl;
f = a * b;
f.printComplex();
/* Divide two complex numbers */
cout << "Division of a and b : " << endl;
g = a / b;
g.printComplex();
/*Print out lenght // How do I implement the lenght function ???
cout<<"Lenght of complex numbers"<<endl;
return */
Length = g.Length {
cout << "Output the lenght of two vecrors" << Length << endl;
}
#endif
}
|