Confusing compile error

P

Pmb

I'm confused as to what the compiler error message I'm getting is refering
to. Can someone take a gander and let me know what I did wrong? The program
is below. When I compile it I get the following error

______________________________
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\complex\temp.cpp:
Error E2333 d:\temp\complex\temp.cpp 73: Class member
'Complex::conjugate(Complex)' declared outside its class
Error E2040 d:\temp\complex\temp.cpp 74: Declaration terminated incorrectly
*** 2 errors in Compile ***

Tool completed with exit code 1
______________________________

Did I screw up with the function which determines the





___________________________________
#include <iostream.h>
#include <math.h>

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex &operator=( Complex & );
bool operator==( Complex & );
private:
Complex conjugate( Complex );
float magnitude( Complex );
float re;
float im;
};

ostream &operator<<( ostream &output, Complex &z )
{
float x = z.re, y = z.im;

if ( y == 0 ) // z = a
output << x;
else if ( x == 0 && y > 0 ) // z = i b
output << "i " << y;
else if ( x == 0 && y < 0 ) // z = - i b
output << "- i " << -y;
else if ( x > 0 && y > 0 ) // z = a + i b
output << x << " + i " << z.im;
else if ( x > 0 && y < 0 ) // z = a - i b
output << x << " - i " << -y;

return output;
}

Complex::Complex( float a, float b )
: re( a ), im( b) { }

Complex Complex::eek:perator+( Complex &z )
{
Complex sum;

sum.re = re + z.re;
sum.im = im + z.im;

return Complex( sum.re, sum.im );
}

Complex Complex::eek:perator-( Complex &z )
{
Complex diff;

diff.re = re - z.re;
diff.im = im - z.im;

return Complex( diff.re, diff.im );
}

Complex& Complex::eek:perator=( Complex &z )
{
re = z.re;
im = z.im;

return *this;
}

bool Complex::eek:perator==( Complex &z )
{
return ( re == z.re && im == z.im );
}

Complex Complex::conjugate( Complex z );
{
return Complex( z.re, -z.im);
}

float Complex::magnitude( Complex z )
{
float x = z.re, y.im;

return sqrt( x*x + y*y );
}

int main()
{
Complex z( 3, 4 );

cout << "z = " << z << endl;
cout << "z* = " << conjugate( z ) << endl:

return 0;
}

___________________________________
 
V

Victor Bazarov

Pmb said:
I'm confused as to what the compiler error message I'm getting is refering
to. Can someone take a gander and let me know what I did wrong? The program
is below. When I compile it I get the following error

______________________________
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\complex\temp.cpp:
Error E2333 d:\temp\complex\temp.cpp 73: Class member
'Complex::conjugate(Complex)' declared outside its class
Error E2040 d:\temp\complex\temp.cpp 74: Declaration terminated incorrectly
*** 2 errors in Compile ***

Tool completed with exit code 1
______________________________

Did I screw up with the function which determines the
[...]

Complex Complex::conjugate( Complex z );
^^^^
What's that there, at the end of the line?
{
return Complex( z.re, -z.im);
}

Victor
 
P

Pmb

Victor Bazarov said:
Pmb said:
I'm confused as to what the compiler error message I'm getting is refering
to. Can someone take a gander and let me know what I did wrong? The program
is below. When I compile it I get the following error

______________________________
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\complex\temp.cpp:
Error E2333 d:\temp\complex\temp.cpp 73: Class member
'Complex::conjugate(Complex)' declared outside its class
Error E2040 d:\temp\complex\temp.cpp 74: Declaration terminated incorrectly
*** 2 errors in Compile ***

Tool completed with exit code 1
______________________________

Did I screw up with the function which determines the
[...]

Complex Complex::conjugate( Complex z );
^^^^
What's that there, at the end of the line?
{
return Complex( z.re, -z.im);
}

Sorry but I have no idea what you're point is. Please be more precise.

Pmb
 
V

Victor Bazarov

Pmb said:
Victor Bazarov said:
Pmb said:
I'm confused as to what the compiler error message I'm getting is refering
to. Can someone take a gander and let me know what I did wrong? The program
is below. When I compile it I get the following error

______________________________
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\complex\temp.cpp:
Error E2333 d:\temp\complex\temp.cpp 73: Class member
'Complex::conjugate(Complex)' declared outside its class
Error E2040 d:\temp\complex\temp.cpp 74: Declaration terminated incorrectly
*** 2 errors in Compile ***

Tool completed with exit code 1
______________________________

Did I screw up with the function which determines the
[...]

Complex Complex::conjugate( Complex z );
^^^^
What's that there, at the end of the line?
{
return Complex( z.re, -z.im);
}

Sorry but I have no idea what you're point is. Please be more precise.

Remove the semicolon.
 
D

Dave Townsend

PMB,

these functions don't seem to be correctly
designed, the conjugate and magnitude should
be using the object, not another "z" value.

ie,
Complex Complex::conjugate( );
{
return Complex( re, - im);
}

float Complex::magnitude( )
{
float x = re;
float y = im;

return sqrt( x*x + y*y );
}
 
P

Pmb

Dave Townsend said:
PMB,

these functions don't seem to be correctly
designed, the conjugate and magnitude should
be using the object, not another "z" value.

ie,
Complex Complex::conjugate( );
{
return Complex( re, - im);
}

float Complex::magnitude( )
{
float x = re;
float y = im;

return sqrt( x*x + y*y );
}

They work fine. They do use the object. The class is defined as

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex operator=( Complex & );

float getRe() { return re; };
float getIm() { return im; };
Complex conjugate();
float magnitude();
private:
float re;
float im;
};


Thanks

Pmb
 
K

Klaus Eichner

Pmb said:
I'm confused as to what the compiler error message I'm getting is refering
to.
[snip]

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
d:\temp\complex\temp.cpp:
Error E2333 d:\temp\complex\temp.cpp 73: Class member
'Complex::conjugate(Complex)' declared outside its class
Error E2040 d:\temp\complex\temp.cpp 74: Declaration terminated incorrectly
*** 2 errors in Compile ***

Tool completed with exit code 1
[snip]

Complex Complex::conjugate( Complex z );
see post by Victor Bazarov to fix the error on the above line
{
return Complex( z.re, -z.im);
}
[snip]

int main()
{
Complex z( 3, 4 );

cout << "z = " << z << endl;
cout << "z* = " << conjugate( z ) << endl:
the colon (":") at the end of the above line must be replaced by a
semi-colon (";")
 
V

Victor Bazarov

Dave Townsend said:
PMB,

these functions don't seem to be correctly
designed, the conjugate and magnitude should
be using the object, not another "z" value.

ie,
Complex Complex::conjugate( );

You might consider watching your semicolons too.
{
return Complex( re, - im);
}
[...]
 
R

Richard Herring

Note that for serious numerical code, this is _not_ the way to code
complex::abs(). Your operator/ has a similar problem.

(Yes, I know this is only a pedagogical toy. All the same, it's not a
good idea to propagate bad practice.)
They work fine.
Now.

They do use the object.

But they didn't in your earlier posting.
The class is defined

- now -
as

class Complex {
friend ostream &operator<<( ostream &, Complex & );
public:
Complex( float = 0.0, float = 0.0 );
Complex operator+( Complex & );
Complex operator-( Complex & );
Complex operator*( Complex & );
Complex operator/( Complex & );
Complex operator=( Complex & );

float getRe() { return re; };
float getIm() { return im; };
Complex conjugate();
float magnitude();
private:
float re;
float im;
};

but originally you had
 
P

Pmb

Richard Herring said:
Note that for serious numerical code, this is _not_ the way to code
complex::abs(). Your operator/ has a similar problem.

I can't help but wonder why you think that this comment of yours is useful.
It is about as useful as "No sa!"

A little advice - If you want to be helpful then back up a claim that
something is not right and then give what you hold to be correct. What I
gave is not incorrect. It works just fine. If you meant that it is
inefficient, i.e. not as computationally speedy as something else you have
in mind, then that comes into the relm of being a tradeoff between
efficiency and readability/maintainability. I may not be interested in an
efficient program. I may only be interested in an easy to write program.
Next time please clarify and justify your remarks.
(Yes, I know this is only a pedagogical toy. All the same, it's not a
good idea to propagate bad practice.)

Prove that this is a bad practice.

<snipped unhelpful comments>

Pmb
 
P

Pmb

This particular class and member function was never intended to be anything
other than something for me to use to learn about classes and C++ etc. Upon
inspection I don't recall why I didn't write the member function as

float Complex::magnitude( )
{
return sqrt( re*re + im*im );
}

But I suspect it has to do with me dabbling with how to implement member
functions and I simply used Complex numbers and magnitude as an obvious
example. However I, personally, would never label such a function "abs()"
since sqrt( re*re + im*im );is called the "modulus" in math circles.

Pmb
 
R

Richard Herring

Pmb said:
I can't help but wonder why you think that this comment of yours is useful.
It is about as useful as "No sa!"

It's intended to make you think.
A little advice - If you want to be helpful then back up a claim that
something is not right and then give what you hold to be correct. What I
gave is not incorrect. It works just fine.

Sure about that?

#include <ostream>
#include <iostream>
#include "your-complex-class.h"

int main()
{
complex x(0.0, 1e35);
float a = x.magnitude();
std::cout << x << ' ' << a << ' \n';
}
If you meant that it is
inefficient, i.e. not as computationally speedy as something else you have
in mind, then that comes into the relm of being a tradeoff between
efficiency and readability/maintainability. I may not be interested in an
efficient program. I may only be interested in an easy to write program.
Next time please clarify and justify your remarks.

This is Usenet. My remarks are worth every penny you paid for them.
Prove that this is a bad practice.

See example.
 
P

Pmb

Richard Herring said:
It's intended to make you think.

Comments such as yours are always unhelpful nd your response to constructive
criticism is even more unhelpful
Sure about that?

#include <ostream>
#include <iostream>
#include "your-complex-class.h"

int main()
{
complex x(0.0, 1e35);
float a = x.magnitude();
std::cout << x << ' ' << a << ' \n';
}

Another useless comment. Seems that you want to hint, rather than state,
what you mean. Seems that you now are trying to hint that one should take
the time to do a test to see if a number is real before taking the modulus.
Hardlly worth the effort, especially since you've yet to justify the
effort.

This is Usenet. My remarks are worth every penny you paid for them.

Yes, You're correct. They are worth nothing. I will assum that is par for
the course for you from now on


Plonk!

Pmb
 
J

Jeff Relf

[ Posted & e-mailed to Pete ( PMB ) ]

Hi Richard Herring, ( and Pete )

Re: Your complex x ( 0.0, 1e35 );

Are you hinting that one should use double variables ?

Or are you saying that you should check for overflows ?

Either way,
I don't think that was Pete's main concern at this point.

As you yourself said, your remarks are,
" worth every penny [ Pete ] paid for them. "
 
P

Pmb

Jeff Relf said:
[ Posted & e-mailed to Pete ( PMB ) ]

Hi Richard Herring, ( and Pete )

Re: Your complex x ( 0.0, 1e35 );

Are you hinting that one should use double variables ?

Or are you saying that you should check for overflows ?

Either way,
I don't think that was Pete's main concern at this point.

As you yourself said, your remarks are,
" worth every penny [ Pete ] paid for them. "

He seems to think that, given the complex number z = x + i y, that its worth
testing as to whether x = 0 or y = 0 since then the modulus is either the
magnitude of x ( i y = 0) or the magnitude of y (if x = 0). However that
requires testing whether a floating point number is zero. In all numerical
applications that I can think of which use complex numbers such a test is a
waste of time. One should never test to see if a floating point number is
zero.It is quite unlikely that a complex number is either purely real of
purely imaginary. At least in all applications that come to mind. It simply
isn't worth the test since the occasion will never come up when determining
the modulus of z.

Pmb
 
J

Jeff Relf

Hi Pete ( Pmb ), [ Posted & e-mailed ]

I was looking in <math.h> when I was this:

// Definition of a _complex struct
// to be used by those who use cabs ( );
// and want type checking on their argument

// real and imaginary parts
struct _complex { double x, y ; } ;

_CRTIMP double __cdecl _cabs ( struct _complex );

And in the help, I found this:

float, 4 bytes, no other name, 3.4E +/- 38 ( 7 digits )
double, 8 bytes, no other name, 1.7E +/- 308 ( 15 digits )
 
D

Dave Moore

Pmb said:
Comments such as yours are always unhelpful nd your response to constructive
criticism is even more unhelpful

Actually, his comments were quite helpful if you take the time to
understand them ... Your "criticism" was not constructive, it was more
like "petulant and whining" if you ask me ... in any case, you are
entitled to your opinion, but don't scold someone who is trying to
help you.

Another useless comment. Seems that you want to hint, rather than state,
what you mean. Seems that you now are trying to hint that one should take
the time to do a test to see if a number is real before taking the modulus.
Hardlly worth the effort, especially since you've yet to justify the
effort.

Dude .. you need to back off .. Richard has done nothing but give you
good advice thus far .. don't chastise him because he didn't take the
time to spell it out for you. Even after you scolded him the first
time he still tried to help you understand why the code you posted is
flawed (and boy is it), he still posted again to help you see the
problem. If we (the usenet community) are going to take the time to
try to help you, you should commit a bit of time and thought to
understanding those comments. You purport to be some kind of "math
guy", but there is a lot more to writing good code than just knowing
the underlying math. His comments were well-meaning and justified ...
look in "Numerical Recipes" or another good reference on techniques
for numerical computations to help understand why. You might want to
google "const correctness" or just take a look at the C++ FAQ while
you're at it .. it might help you to improve your code and understand
other people's helpful comments.

Anyway, it takes time to type these posts, and we are only trying to
help you .. respect that and take some time trying to understand them.

Dave Moore
 
R

Richard Herring

Pmb said:
Jeff Relf said:
[ Posted & e-mailed to Pete ( PMB ) ]

Hi Richard Herring, ( and Pete )

Re: Your complex x ( 0.0, 1e35 );

Are you hinting that one should use double variables ?

Or are you saying that you should check for overflows ?

Either way,
I don't think that was Pete's main concern at this point.

As you yourself said, your remarks are,
" worth every penny [ Pete ] paid for them. "

He seems to think that, given the complex number z = x + i y, that its worth
testing as to whether x = 0 or y = 0 since then the modulus is either the
magnitude of x ( i y = 0) or the magnitude of y (if x = 0).

No he doesn't. My objection has nothing to do with whether it's pure
real or pure imaginary. Did you try running my example?
However that
requires testing whether a floating point number is zero. In all numerical
applications that I can think of which use complex numbers such a test is a
waste of time. One should never test to see if a floating point number is
zero.

Not true. There are many occasions on which it would be better to test
if it's smaller than some epsilon; there are others on which a
comparison with zero is quite legitimate.
 
R

Richard Herring

Jeff Relf <[email protected]> said:
[ Posted & e-mailed to Pete ( PMB ) ]

Hi Richard Herring, ( and Pete )

Re: Your complex x ( 0.0, 1e35 );

Are you hinting that one should use double variables ?

No (though that would be a good idea in any case.)
Or are you saying that you should check for overflows ?

No. I'm saying that you should code so as not to produce unnecessary
overflows.
Either way,
I don't think that was Pete's main concern at this point.

As I acknowledged in my original post.
As you yourself said, your remarks are,
" worth every penny [ Pete ] paid for them. "
 
R

Richard Herring

Pmb said:
Comments such as yours are always unhelpful nd your response to constructive
criticism is even more unhelpful

You're the one asking for advice here, sunshine.
Another useless comment. Seems that you want to hint, rather than state,
what you mean. Seems that you now are trying to hint that one should take
the time to do a test to see if a number is real before taking the modulus.

Wrong again. Seems that you are trying to guess what I mean, with no
success.

Have you tried running my example?
Hardlly worth the effort, especially since you've yet to justify the
effort.

Have you tried running it?
Yes, You're correct. They are worth nothing. I will assum that is par for
the course for you from now on


Plonk!

I doubt it. The pmb "killfile" is a source of much amusement in some
other groups.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top