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;
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
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.
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;
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
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;
}
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.