Error Message Help

This is a discussion on Error Message Help within the C++ Programming forums, part of the General Programming Boards category; Hello, when i try to compile this program (it has three parts) I receive an error message that confuses me. ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    21

    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.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    >> #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

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    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?

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    your missing a semi colon like the error message says

    Code:
    class Foo {
    
    /* stuff */
    };

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    Wow, I am slow....hahah I didn't even catch that.


    Thanks!

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    21
    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.
    Last edited by deedlit; 09-26-2004 at 04:31 PM.

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Don't know. What doesn't work?

    Also,
    >>Complex Complex:perator+( Complex &);
    >>Complex Complex:perator-( 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'
    Last edited by Hunter2; 09-26-2004 at 06:46 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21