473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Overloading operator fails ?

I have a Class for complex operations:

class CComplex
{
public:
inline CComplex() { re=im=0.0; }
inline CComplex operator*(CComplex c) { return
CComplex(re*c.re-im*c.im,re*c.im+im*c.re);}
inline CComplex operator*(double &d) { return CComplex(re*d,im*d);}
double re,im;
};

However when multiplying double * CComplex the operation fails

double m_Phase[8192][8192]; // Phase of Field
double m_Amplitude[8192][8192]; // Amplitude of Field
CComplex m_Field[8192][8192]; // Full electric Field

CComplex Expi(double wt) {return CComplex(cos(wt),sin(wt));}
CComplex Expi(CComplex x) {return

m_Field[x][y]= (m_Amplitude[x][y]) * (Expi(m_Phase[x][y]));
with

error C2677: Binärer Operator '*': Es konnte kein globaler Operator
gefunden werden, der den Typ 'CComplex' akzeptiert (oder keine geeignete
Konvertierung möglich)

(no Operator found that accepts CComplex)

What is going wrong ??

Matthias
Dec 6 '06 #1
9 2041
On Dec 6, 2:20 pm, Matthias Pospiech <matthia...@gmx.dewrote:
I have a Class for complex operations:

class CComplex
{
public:
inline CComplex() { re=im=0.0; }
inline CComplex operator*(CComplex c) { return
CComplex(re*c.re-im*c.im,re*c.im+im*c.re);}
inline CComplex operator*(double &d) { return CComplex(re*d,im*d);}
double re,im;

};However when multiplying double * CComplex the operation fails

double m_Phase[8192][8192]; // Phase of Field
double m_Amplitude[8192][8192]; // Amplitude of Field
CComplex m_Field[8192][8192]; // Full electric Field

CComplex Expi(double wt) {return CComplex(cos(wt),sin(wt));}
CComplex Expi(CComplex x) {return

m_Field[x][y]= (m_Amplitude[x][y]) * (Expi(m_Phase[x][y]));

with

error C2677: Binärer Operator '*': Es konnte kein globaler Operator
gefunden werden, der den Typ 'CComplex' akzeptiert (oder keine geeignete
Konvertierung möglich)
You have defined an operator* for CComplex * double, but not for double
* CComplex. I'm not sure what the solution to this problem is, it might
have something to do with not declaring the operator as a member. I
seem to recall that there is something about it in the FAQ though,
check it out: http://www.parashift.com/c++-faq-lite/.

Aslo, why do you create a complex-class when there already exist on in
the standard library?

--
Erik Wikström

Dec 6 '06 #2
Matthias Pospiech wrote:
I have a Class for complex operations:
what is wrong with std::complex ?
class CComplex
{
public:
inline CComplex() { re=im=0.0; }
use constructor initializer list for member initialization wherever
possible

CComplex() : re(0), im(0) {}
inline CComplex operator*(CComplex c) { return
since it returns new object by value, then it doesn't modify nither
'this' nor c, why is it not

CComplex operator*(CComplex const & c) const

then.

I believe that the general rule for operator is to make +=, -=, *= and
/= members
and all others to declare outside the class.

CComplex & operator*=(CComplex const & c)
{
...
return *this;
}
CComplex(re*c.re-im*c.im,re*c.im+im*c.re);}
there is no CComplex constructor that takes two doubles
inline CComplex operator*(double &d) { return CComplex(re*d,im*d);}
you pass double by reference and objects by value, why?
double re,im;
consider declaring each member on it's own line
};
you need to define a double*CComplex binary operator *

CComplex operator*(double d, CComplex const & c)
>
Dec 6 '06 #3
dasjotre schrieb:
>
> inline CComplex operator*(double &d) { return CComplex(re*d,im*d);}

you pass double by reference and objects by value, why?
what do you mean by 'objects by value' ?
>
you need to define a double*CComplex binary operator *

CComplex operator*(double d, CComplex const & c)
I do not know how to implement such a function. The following does not work

CComplex operator*(double d, CComplex const & c) { return
CComplex(c.re*d,c.im*d);}
>[...].h(102) : error C2804: Binärer Operator '*' hat zu viele Parameter
[...].h(102) : error C2333: 'CComplex::operator *': Fehler in
Funktionsdeklaration; Funktionstext wird übersprungen

I think some working example code would be helpfull.

Matthias

Dec 7 '06 #4
Matthias Pospiech wrote:
<snip>

class CComplex
{
public:

CComplex(): re(0), im(0) {}
CComplex(double d, double c): re(d), im(c) {}
// you don't realy need to define this, but it will
// help you understand the distinction between
// by-value and by-reference
CComplex(CComplex const & cpy)
: re(cpy.re), im(cpy.im)
{
}
CComplex & operator *=(CComplex const & c)
{
re = re*c.re-im*c.im;
im = re*c.im+im*c.re;
return *this;
}
CComplex & operator*=(double d)
{
re *= d;
im *= d;
return *this;
}

private:

double re;
double im;
};
// binary operators outside the class
inline CComplex operator*(double d, CComplex const & c)
{
CComplex ret(c);
ret *= d;
return ret;
}
inline CComplex operator*(CComplex const & c, double d)
{
return (d*c);
}

// try these
void CComplex_passed_by_value(CComplex c){}
void CComplex_passed_by_reference(CComplex & c){}

also look at <complexheader in your standard library
the above code is simmilar to it
I think some working example code would be helpfull.
yes, a compilable code at least. take that as a basic requirement next
time you post here ;)

Dec 7 '06 #5

dasjotre wrote:
yes, a compilable code at least. take that as a basic requirement next
time you post here ;)
sorry, I ment complete code at least.

Dec 7 '06 #6
dasjotre schrieb:
yes, a compilable code at least. take that as a basic requirement next
time you post here ;)
It does compile.

However, I have never seen the following construct before:

CComplex(): re(0), im(0) {}

I do understand what it does, but is there any documentation about this
syntax on the web that I can read ?

Matthias
Dec 7 '06 #7
Matthias Pospiech <ma********@gmx.dewrote:
However, I have never seen the following construct before:

CComplex(): re(0), im(0) {}

I do understand what it does, but is there any documentation about this
syntax on the web that I can read ?
Try searching for something like "initializer list" or "initialization
list". If you have a reference or a const member in your class, it is
the only way to initialize them, and if you have a derived class, it is
used to call a base class constructor if you need to pass a parameter to
it.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Dec 7 '06 #8
The Code based on your post (see bottom of this post) compiles and
works. However other previous working code now fails - and I do not
understand why...

The following class (with #include <MpMath.h>)

class CComplexVector2
{
public:
/* Konstruktoren */
inline CComplexVector2() { m[1]=&x;m[2]=&y; x.re=y.re=x.im=y.im=0.0; }
inline CComplexVector2(const CComplexVector2 &c) { m[1]=&x;m[2]=&y;
x=c.x;y=c.y;}
inline CComplexVector2(const double &d1, const double &d2) {
m[1]=&x;m[2]=&y; x.re=d1;y.re=d2;x.im=y.im=0.0;}
inline CComplexVector2(const CComplex &d1, const CComplex &d2) {
m[1]=&x;m[2]=&y; x=d1;y=d2;}
/* Operatoren */
inline CComplexVector2& operator=(const CComplexVector2 &c) {
m[1]=&x;m[2]=&y; x=c.x;y=c.y; return *this;}
inline CComplexVector2 operator+(CComplexVector2 c) { return
CComplexVector2(x+c.x,y+c.y);}
inline CComplexVector2 operator+(CVector2 c) { return
CComplexVector2(x+c.x,y+c.y);}
CComplex x,y;
CComplex *m[3];
};

fails with
>D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightMod ulator2D\um.h(185) :
error C2678: Binärer Operator '+': Es konnte kein Operator gefunden
werden, der einen linksseitigen Operanden vom Typ 'CComplex' akzeptiert
(oder keine geeignete Konvertierung möglich)
1>
D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightModu lator2D\MpMath.h(51):
kann 'CComplex operator +(double,const CComplex &)' sein
1>
D:\Matthias\CPP\Uwe\C++\LightModulator2D\LightModu lator2D\MpMath.h(52):
oder "CComplex operator +(const CComplex &,double)"
1 bei Anpassung der Argumentliste '(CComplex, CComplex)'
The CComplex class now looks like (MpMath.cpp)
class CComplex
{
public:
/* Überladen Konstruktoren */
CComplex(): re(0), im(0) {}
//CComplex(const double &d, const double &c): re(d), im(c) {}
CComplex(const double &d1, const double &d2) {re=d1;im=d2;}
CComplex(const CComplex &c) { re=c.re;im=c.im;}
CComplex(const double &c) { re=c;im=0.0;}

/* Multiplikation */
CComplex & operator *=(CComplex const & c){re = re*c.re-im*c.im; im =
re*c.im+im*c.re; return *this;}
CComplex & operator *=(double d) {re *= d;im *= d; return *this;}
/* Addition */
CComplex & operator +=(CComplex const &c) {re=re+c.re; im=im+c.im;
return *this;}
CComplex & operator +=(double d) {re+=d; return *this;}
/* Subtraction */
CComplex & operator -=(CComplex const &c) {re= re-c.re; im=im-c.im;
return *this;}
CComplex & operator -=(double d) {re-=d; return *this;}
/* Division */
CComplex & operator /=(CComplex const &c) {
double b=c.re*c.re+c.im*c.im;
re=(re*c.re+im*c.im)/b;im=(im*c.re-re*c.im)/b; return *this;
};
CComplex & operator /=(double d){re=re/d;im=im/d;return *this;}
/* Invers */
CComplex operator -() { return CComplex(-re,-im);}
public:
double re;
double im;
};

// binary operators outside the class
// Necessary to have double * complex AND complex * double
inline CComplex operator*(double d, CComplex const & c){CComplex
ret(c);ret *= d;return ret;}
inline CComplex operator*(CComplex const & c, double d){return (d*c);}
inline CComplex operator+(double d, CComplex const & c){CComplex
ret(c);ret += d;return ret;}
inline CComplex operator+(CComplex const & c, double d){return (d+c);}
inline CComplex operator-(double d, CComplex const & c){CComplex
ret(c);ret -= d;return ret;}
inline CComplex operator-(CComplex const & c, double d){return (d-c);}
inline CComplex operator/(double d, CComplex const & c){CComplex
ret(c);ret /= d;return ret;}
inline CComplex operator/(CComplex const & c, double d){return (d/c);}
Dec 8 '06 #9
Hello,

Matthias Pospiech wrote:
The Code based on your post (see bottom of this post) compiles and
works. However other previous working code now fails - and I do not
understand why...
Because now some overloads of operator* and others are missing, esp. the
operator* for two CComplex objects. There is no automatism in C++ to
provide e.g. operator*(CComplex,CComplex) from
CComplex::operator*=(CComplex)
class CComplex
{
...
public:
double re;
double im;
};

// binary operators outside the class
// Necessary to have double * complex AND complex * double
inline CComplex operator*(double d, CComplex const & c){CComplex
ret(c);ret *= d;return ret;}
inline CComplex operator*(CComplex const & c, double d){return (d*c);}
The following overload is plainly missing, analogously for other
operators. You might even get away with this missing one alone, since a
double can be converted via implicit constructor to CComplex. Due to
inlining all modern compilers should produce equivalent results here.
So it might be recommendable to provide only this one overload, and
rely on conversion for the others:

inline CComplex operator*(CComplex const & c, CComplex const & d)
{
CComplex z(c);
z*=d;
return z;
}

Now you might be tempted to move this operator back into the class as
member function, but then it will fail again to provide all usually
wanted overloads. For member functions to match the left hand side of
the operator must be of the class, there are no conversions considered.

Bernd Strieder

Dec 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Laxman | last post by:
==C.H <== #include <fstream.h> class C { public: C(int i); friend ofstream& operator<<(ofstream& os, const C&); }; ------------------------------------------------------------ ==B.H <==
3
by: jim.brown | last post by:
The attached code implements a test class to show an error in overloading operator=. This code works on Windows with Visual Studio and simpler cases work with gcc 3.3.2 on Solaris 9. On Windows,...
5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
10
by: chetan | last post by:
Hello All ! In c++, are basic data type int,char,long,..,etc. classes ? And if so, How can i overload new and delete operators for this this objects ? Thanks in advance ! Chetan
8
by: andrew browning | last post by:
I am getting an istream overloading error that looks like this: error: no match for 'operator>>' in 'ins >> target->abrowning_rational::Rational::numerator' Below is the .h file: #ifndef...
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
11
by: Zilla | last post by:
I have the following simple program. I just want to be able to do math operations (+, -, =)on Timer sublcasses, but want to handle cases where either rhs or lhs is an intrinsic value, However, the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.