debugging class

I have a program I am working on, it's a class. I am having some trouble with it when I build it a keep getting errors that I don't understand. I will post the program below. Let me know what you think I need to fix:

#include <iostream>
#include <cmath>
using namespace std;

class Complex{
private:
double real,img;
public:
Complex();
Complex(double real,double img);
Complex(const Complex&);

void setReal(double);
void setImg(double);

double getReal()const ;
double getImg()const ;

Complex add(Complex&);
Complex sub(Complex&);
Complex mult(Complex&);
Complex div(Complex&);
Complex conjugate(); //Returns the complex conjugate of the invoking object
double modulus()const; //Returns the modulus of the invoking object


void print()const;

void read();




};

Complex::Complex();

Complex::Complex(double real, double img);

void Complex::setReal(double r){
real=r;
}
void Complex::setImg(double i){
img=i;
}
double Complex::getReal()const{
return real;
}
double Complex::getImg()const{
return img;
}
void Complex::read(){
cin >> real >> img;
}
void print(){
double real;
double img;
cout << real << '+' << img << endl;
}
Complex::add(Complex x){
Complex temp;
temp.real=real+x.real;
temp.img=img+x.img;
return temp;
}
Complex::sub(Complex x){
Complex temp;
temp.real=real-x.real;
temp.img=img-x.img;
return temp;
}
double Complex::mult(Complex x){
Complex temp;
temp.real=real*x.real-img*x.img;
temp.img=real*x.img+img*x.real;
return temp.real;
return temp.img;

}
double Complex::conjugate(Complex x);{
temp.img=(-1)*x.img;
//return temp.img;
}

//double modulus(double r, double i){}


int main()
{
//x.setReal(3);
//x.setImg(4);
cout << "Enter the Real component and then enter the Imaginary component"<<endl;
y.read;
z=x.add(y);
cout << "The sum is" << z.print << endl;
z=x.sub(y);
cout << "The difference is" << z.print << endl;
z=x.mult(y);
cout << "The product is" << z.print << endl;
z=x.div(y);
cout << "The quotient is" << z.print << endl;
cout <<"The modulus is" << mod << endl;

return 0;
}
I got these:
test.cpp 36 error: declaration of `Complex::Complex()' outside of class is not definition
test.cpp 38 error: declaration of `Complex::Complex(double, double)' outside of class is not definition
test.cpp 60 error: ISO C++ forbids declaration of `add' with no type
test.cpp 60 error: prototype for `int Complex::add(Complex)' does not match any in class `Complex'
test.cpp 19 error: candidate is: Complex Complex::add(Complex&)
test.cpp In member function `int Complex::add(Complex)':
test.cpp 64 error: cannot convert `Complex' to `int' in return
test.cpp 66 error: ISO C++ forbids declaration of `sub' with no type
test.cpp 66 error: prototype for `int Complex::sub(Complex)' does not match any in class `Complex'
test.cpp 20 error: candidate is: Complex Complex::sub(Complex&)
test.cpp In member function `int Complex::sub(Complex)':
test.cpp 70 error: cannot convert `Complex' to `int' in return
test.cpp 72 error: prototype for `double Complex::mult(Complex)' does not match any in class `Complex'
test.cpp 21 error: candidate is: Complex Complex::mult(Complex&)
test.cpp 80 error: prototype for `double Complex::conjugate(Complex)' does not match any in class `Complex'
test.cpp 23 error: candidate is: Complex Complex::conjugate()
test.cpp 80 error: declaration of `double Complex::conjugate(Complex)' outside of class is not definition
test.cpp 80 error: expected unqualified-id before '{' token
test.cpp In function `int main()':
test.cpp 93 error: `y' was not declared in this scope
test.cpp 94 error: `z' was not declared in this scope
test.cpp 94 error: `x' was not declared in this scope
test.cpp 102 error: `mod' was not declared in this scope


Lines 36 and 38: empty implementations should be {}, not ;.
Line 60: did you mean "Complex Complex::add(Complex &x){"?
Line 66: did you mean "Complex Complex::sub(Complex &x){"?
Line 72: did you mean "double Complex::mult(Complex &x){"?
Line 80: did you mean "double Complex::conjugate(){"?
Lines 93 and 94 twice: add "Complex x,y,z;" at the beginning of main().
Line 102: I don't know.
Last edited on
I made the corrections, but now I am getting different bugs. Here they are:
cs124project1.cpp(83) : error C2664: 'Complex::Complex(const Complex &)' : cannot convert parameter 1 from 'double' to 'const Complex &'
1> Reason: cannot convert from 'double' to 'const Complex'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
cs124project1\cs124project1\cs124project1.cpp(84) : error C2664: 'Complex::Complex(const Complex &)' : cannot convert parameter 1 from 'double' to 'const Complex &'
1> Reason: cannot convert from 'double' to 'const Complex'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
cs124project1\cs124project1.cpp(88) : error C2065: 'temp' : undeclared identifier
cs124project1\cs124project1\cs124project1.cpp(88) : error C2228: left of '.img' must have class/struct/union
1> type is ''unknown-type''
cs124project1\cs124project1\cs124project1.cpp(88) : error C2065: 'x' : undeclared identifier
cs124project1\cs124project1.cpp(88) : error C2228: left of '.img' must have class/struct/union
1> type is ''unknown-type''
cs124project1\cs124project1.cpp(89) : error C2065: 'temp' : undeclared identifier
cs124project1\cs124project1.cpp(89) : error C2228: left of '.img' must have class/struct/union
1> type is ''unknown-type''
cs124project1\cs124project1.cpp(101) : error C3867: 'Complex::read': function call missing argument list; use '&Complex::read' to create a pointer to member
cs124project1\cs124project1.cpp(103) : error C3867: 'Complex::print': function call missing argument list; use '&Complex::print' to create a pointer to member
cs124project1.cpp(105) : error C3867: 'Complex::print': function call missing argument list; use '&Complex::print' to create a pointer to member
cs124project1.cpp(107) : error C3867: 'Complex::print': function call missing argument list; use '&Complex::print' to create a pointer to member
cs124project1.cpp(109) : error C3867: 'Complex::print': function call missing argument list; use '&Complex::print' to create a pointer to member

Here is the code:
#include <iostream>
#include <cmath>
using namespace std;

class Complex{
private:
double real,img;
public:
Complex();
Complex(double real,double img);
Complex(const Complex&);

void setReal(double);
void setImg(double);

double getReal()const ;
double getImg()const ;

Complex add(Complex&);
Complex sub(Complex&);
Complex mult(Complex&);
Complex div(Complex&);
Complex conjugate(); //Returns the complex conjugate of the invoking object
double modulus()const; //Returns the modulus of the invoking object


void print()const;

void read();




};

Complex::Complex(){}

Complex::Complex(double real, double img){}

void Complex::setReal(double r){
real=r;
}
void Complex::setImg(double i){
img=i;
}
double Complex::getReal()const{
return real;
}
double Complex::getImg()const{
return img;
}
void Complex::read(){
cin >> real >> img;
}
void print(){
double real;
double img;
cout << real << '+' << img << endl;
}
Complex Complex::add(Complex &x){
Complex temp;
temp.real=real+x.real;
temp.img=img+x.img;
return temp;
}
Complex Complex::sub(Complex &x){
Complex temp;
temp.real=real-x.real;
temp.img=img-x.img;
return temp;
}
Complex Complex::mult(Complex &x){
Complex temp;
temp.real=real*x.real-img*x.img;
temp.img=real*x.img+img*x.real;
return temp.real;
return temp.img;

}
Complex Complex::conjugate(){
temp.img=(-1)*(x.img);
return temp.img;
}

//double modulus(double r, double i){}


int main()
{
Complex x,y,z;
x.setReal(3);
x.setImg(4);
cout << "Enter the Real component and then enter the Imaginary component"<<endl;
y.read;
z=x.add(y);
cout << "The sum is" << z.print << endl;
z=x.sub(y);
cout << "The difference is" << z.print << endl;
z=x.mult(y);
cout << "The product is" << z.print << endl;
z=x.div(y);
cout << "The quotient is" << z.print << endl;
//cout <<"The modulus is" << mod << endl;

return 0;
}






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
Complex x,y,z;
x.setReal(3);
x.setImg(4);
cout << "Enter the Real component and then enter the Imaginary component"<<endl;
y.read; //missing ()
z=x.add(y);
//The next lines that put z.print into the cout stream are incorrect
cout << "The sum is" << z.print << endl;
z=x.sub(y);
cout << "The difference is" << z.print << endl;
z=x.mult(y);
cout << "The product is" << z.print << endl; 
z=x.div(y);
cout << "The quotient is" << z.print << endl;
//cout <<"The modulus is" << mod << endl;

return 0;




1
2
3
4
5
6
7
Complex Complex::mult(Complex &x){
Complex temp;
temp.real=real*x.real-img*x.img;
temp.img=real*x.img+img*x.real;
return temp.real; //two return statements and neither of them are correct
return temp.img; //you are supposed to  return a complex not a double
}


1
2
3
4
5
6
//Where are your variable declarations in this function??
//and it should be returning a complex not a double
Complex Complex::conjugate(){
temp.img=(-1)*(x.img);
return temp.img;
}

You really ought to use the code tags when you post code.
Here is what my code looks like now:

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
 #include <iostream>
#include <cmath>
using namespace std;

class Complex{
private:
	double real,img;
public:
	Complex();
	Complex(double real,double img);
	Complex(const Complex&);

	void setReal(double);
	void setImg(double);

	double getReal()const ;
	double getImg()const ; 

	Complex add(Complex&);
	Complex sub(Complex&);
	Complex mult(Complex&);
	Complex div(Complex&);
	Complex conjugate(); //Returns the complex conjugate of the invoking object
	double modulus()const; //Returns the modulus of the invoking object
	
	
	void print()const;

	void read();
	



};

Complex::Complex(): real(0), img(0){}

Complex::Complex(double r, double i): real(r), img(i){}
	
void Complex::setReal(double r){
	real=r;
}
void Complex::setImg(double i){
	img=i;
}
double Complex::getReal()const{
	return real;
}
double Complex::getImg()const{
	return img;
}
void read(char ch){	
	cin >> r >> ch >> i;
}
void print(){	
	cout << r << '+' << i <<'i'<< endl;
}
 Complex Complex::add(Complex &x){
	Complex temp;
	temp.real=real+x.real;
	temp.img=img+x.img;
	return temp;
}
 Complex Complex::sub(Complex &x){
	Complex temp;
	temp.real=real-x.real;
	temp.img=img-x.img;
	return temp;
}
Complex Complex::mult(Complex &x){
	Complex temp;
    temp.real=real*x.real-img*x.img;
    temp.img=real*x.img+img*x.real;
	return temp;

}
Complex Complex::conjugate(){
	Complex temp;
	temp.img=(-1)*(x.img);
	return temp;
}

//double modulus(double r, double i){}


//int main()
//{
//	Complex x,y,z;
//	x.setReal(3);
//	x.setImg(4);
//cout << "Enter the Real component and then enter the Imaginary component"<<endl;
//y.read();
//z=x.add(y);
//cout << "The sum is" << z.print() << endl;
//z=x.sub(y);
//cout << "The difference is" << z.print() << endl;
//z=x.mult(y);
//cout << "The product is" << z.print() << endl;
//z=x.div(y);
//cout << "The quotient is" << z.print() << endl;
////cout <<"The modulus is" << mod << endl;

	/*return 0;*/
//}


	



	


Here are the problems:
project1.cpp(60) : error C2065: 'r' : undeclared identifier
project1.cpp(60) : error C2065: 'i' : undeclared identifier
project1.cpp(63) : error C2065: 'r' : undeclared identifier
project1.cpp(63) : error C2065: 'i' : undeclared identifier
project1.cpp(86) : error C2065: 'x' : undeclared identifier
How am I supposed to declare the identifiers when I (thought) they were already in the constructor?
project1.cpp(86) : error C2228: left of '.img' must have class/struct/union
This one I don't know what to do with.
Last edited on
How about passing Complex::conjugate() some parameters?
The errors are pretty self-explanatory.
I added this:

1
2
3
4
Complex Complex::conjugate(Complex &x){
	Complex temp;
	temp.img=(-1)*(x.img);
	return temp;


And got this:
project1.cpp(84) : error C2511: 'Complex Complex::conjugate(Complex &)' : overloaded member function not found in 'Complex'

Would this mean I need a constructor along these lines?:
Complex::Complex(double i):img(i){}
You have a function declaration and a function definition. You updated the definition, now update the declaration.
Topic archived. No new replies allowed.