-
Error Message Help
Hello,
when i try to compile this program (it has three parts) I receive an error message that confuses me. Below are the code and error message.
Code:
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex x(2, 3), y(4, 5), z;
x.printComplex();
cout << " + ";
y.printComplex();
cout << " = ";
x.add(y);
x.printComplex();
cout << '\n';
x.setComplexNumber(10, 1); //reset Real
y.setComplexNumber(11, 5); //reset Imaginery
x.printComplex();
cout << " - ";
y.printComplex();
cout << " = ";
x.subtract(y);
x.printComplex();
cout << '\n';
x.equal(y);
cout << endl;
return 0;
}
Code:
#ifndef Complex.h
#define Complex.h
class Complex {
public:
Complex(double = 0.0, double = 0.0); //default constructor
Complex Complex::add(Complex &);
Complex Complex::subtract(Complex &);
void printComplex(void);
void setComplexNumber(double, double);
Complex Complex::equal(Complex &x);
private:
double realPart;
double imagineryPart;
}
#endif
Code:
#include <iostream>
using std::cout;
#include <Complex.h>
Complex::Complex(double real, double imaginery)
{ setComplexNumber(real, imaginery); }
void Complex::add(Complex &)
{
realPart += a.realPart;
imagineryPart =+ a.imagineryPart;
}
void Complex::subtract(Complex &)
{
realPart -= s.realPart;
imagineryPart -= s.realPart;
}
void Complex::printComplex(void)
{ cout << '(' << realPart << ", " << imagineryPart << ')'; }
void setComplexNumber(double rp, dounle ip)
{
realPart = rp;
imagineryPart = ip;
}
bool Complex::equal(Complex &x) {
return ((realPart == x.realPart) &&
(imagineryPart == x.imagineryPart));
}
The error msg says:
complex classes\complex.h(4) : warning C4067: unexpected tokens following preprocessor directive - expected a newline
complex classes\complex.h(5) : error C2008: '.' : unexpected in macro definition
Complex Classes\ComplexClient.cpp(11) : error C2628: 'h' followed by 'int' is illegal (did you forget a ';'?)
I do not understand what those mean. Can anyone help me? Thanks so much, I really appreciate it a lot.
-
>> #ifndef Complex.h
>> #define Complex.h
you cant use "." in your macros, use underscores instead. Also, macro's are usually defined in caps by convention. try:
#ifndef COMPLEX_H
#define COMPLEX_H
-
Thank you very much! that took care of two of three of the errors. But why won't it let me have the int after a .h file?
-
your missing a semi colon like the error message says
Code:
class Foo {
/* stuff */
};
-
Wow, I am slow....hahah I didn't even catch that.
Thanks!
-
Hi, thanks for reading my message. I've changed my code a bit and I get it to compile but the math is wrong.
Code:
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex x;
Complex y( 3, 4);
Complex z( 6, 2);
cout << "x: ";
x.printComplex();
cout << "\ny: ";
y.printComplex();
cout << "\nz: ";
z.printComplex();
x = y + z;
cout << "\n\nx = y + z:\n";
x.printComplex();
cout << " = ";
y.printComplex();
cout << " + ";
z.printComplex();
x = y - z;
cout << "\n\nx = y - z:\n";
x.printComplex();
cout << " = ";
y.printComplex();
cout << " - ";
z.printComplex();
cout << endl;
return 0;
}// end main
Code:
#include <iostream>
using std::cout;
#include "Complex.h"
Complex::Complex(double realPart, double imagineryPart)
: real( realPart ),
imaginery( imagineryPart)
{
//empty body
}
//addition
Complex Complex::operator+( Complex &operand2)
{
return Complex( real + operand2.realPart,
imaginery + operand2.imagineryPart );
}//end
//subtraction
Complex Complex::operator-( Complex &operand2)
{
return Complex( real - operand2.realPart,
imaginery - operand2.imagineryPart );
}//end
//display in form (a, b)
void Complex::printComplex() const
{
cout << '(' << realPart << ", " << imagineryPart << ')';
}
//equal or not
bool Complex::equal(Complex &x) {
return ((realPart == x.realPart) &&
(imagineryPart == x.imagineryPart));
}
Code:
#ifndef Complex_H
#define Complex_H
class Complex {
public:
Complex(double = 0.0, double = 0.0); //default constructor
Complex Complex::operator+( Complex &);
Complex Complex::operator-( Complex &);
void printComplex() const;
bool Complex::equal(Complex &x);
private:
double realPart;
double imagineryPart;
double real;
double imaginery;
};
#endif
Could you tell me what you see wrong? My debugger doesn't catch anything but I know that doesn't mean anything besides syntax is ok. Also, how to I implement the "equal" function so that it doesn't just sit there doing nothing? Thanks for your help.
-
Don't know. What doesn't work?
Also,
>>Complex Complex::operator+( Complex &);
>>Complex Complex::operator-( Complex &);
>>bool Complex::equal(Complex &x);
You don't need the Complex:: in these lines. Also, I'm not sure if it's what you intend but if you want 'equal' to be called when someone does (somecomplex) == (another), it should be operator==.
[edit]
OK. You're mixing your 'real' and 'imaginery' and 'realPart' and 'imagineryPart'. You initialize real and imaginery in the constructor and leave realPart and imagineryPart alone, but in your +- operators you're adding realPart and imagineryPart to real and imaginery. Problem is, there's nothing useful contained in realPart and imagineryPart, and that's why your math operations are messed. Just get rid of realPart and imagineryPart and replace them with real and imaginery, and you should be fine (other than the constructor arguments, you can leave the names as realPart and imagineryPart there since they're arguments that aren't used anywhere else).
P.S. It's spelled 'imaginary', not 'imaginery' ;)