Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

problem sa predefinisanjem operatora!!!

[es] :: C/C++ programiranje :: problem sa predefinisanjem operatora!!!

[ Pregleda: 3099 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

refa
Tuzla

Član broj: 60356
Poruke: 59
*.dlp334.bih.net.ba.



Profil

icon problem sa predefinisanjem operatora!!!30.01.2006. u 21:05 - pre 221 meseci
Code:
#include<iostream>
#include<math.h>
using namespace std;

class komplex{
private:
    double real, imag,arg;
public:
    komplex()
    {
        real=0;
        imag=0;
        arg=0;
    }
    ~komplex()
    {}
    komplex(const komplex &);
    komplex& operator=(const komplex &);
    komplex& operator+(const komplex &);
    komplex& operator-(const komplex &);
    komplex& operator*(const komplex &);
    komplex& operator/(const komplex &);
    friend komplex& operator+(const komplex &,const komplex &);
    friend komplex& operator-(const komplex &,const komplex &);
    friend komplex& operator*(const komplex &,const komplex &);
    friend komplex& operator/(const komplex &,const komplex &);
    void set()
    {
        cout<<"real=";
        cin>>real;
        cout<<"imag=";
        cin>>imag;
        cout<<endl;
        arg=atan(imag/real);
    }
    void print()
    {
        cout<<"real="<<real<<endl;
        cout<<"imag="<<imag<<endl;
        cout<<"arg="<<arg<<endl;
    }
    
};
komplex::komplex(const komplex &a)
{
    real=a.real;
    imag=a.imag;
    arg=a.arg;
}

komplex& komplex::operator=(const komplex &a)
{
    real=a.real;
    imag=a.imag;
    arg=a.arg;
    return *this;
}
komplex& komplex::operator+(const komplex &a)
{
    real+=a.real;
    imag+=a.imag;
    arg=atan(imag/real);
    return *this;
}
komplex& operator+(const komplex &a1,komplex &a2)
{
    komplex x(a1);
    x=x+a2;
    return x;//ovako nece
}
komplex& komplex::operator-(const komplex &a)
{
    real-=a.real;
    imag-=a.imag;
    arg=atan(imag/real);
    return *this;
}
komplex& operator-(const komplex &a,komplex &b)
{
    return a-b;//ovako isto nece
}
komplex& komplex::operator*(const komplex &a)
{
    real*=a.real;
    imag*=a.imag;
    arg=atan(imag/real);
    return *this;
}
komplex& operator*(const komplex &a,komplex &b)
{
    return a-b;
}
komplex& komplex::operator/(const komplex &a)
{
    if(a.real || a.imag)
    {
        real/=a.real;
        imag/=a.imag;
        arg=atan(imag/real);
        return *this;
    }
}
komplex& operator/(const komplex &a,komplex &b)
{
    return a/b;
}
int main(int argc,char *argv[])
{
    komplex x,y,z;
    x.set();
    y.set();
    z=y;
    z=x+y;
             x=z*y;
             y=x/z;
    z.print();
    return 0;
}




gdje grijesim uvijek mi javi gresku kad koristim neku od operacija koje sam ja definisao na komplexnim brojevima

Error 1 error C2666: 'komplex::operator +' : 3 overloads have similar conversions d:\c\refa\visual studio 2005\projects\matr-strukt\matr-strukt\program.cpp 68

to je error koji dobijem
ludim, uradio sam sabiranje, oduzimanje i mnozenje matrica, zapravo vi ste mi pomogli dodavanjem friend funkcija i to radi, ali ovo me izludje a ne znam u cemu je problem.

refa
 
Odgovor na temu

DDMM
Dejan D. M. Milosavljevic
Danguba
Gajba, ali ne piva.

Član broj: 2544
Poruke: 89
213.244.197.*

Sajt: www.ddmrm.com


Profil

icon Re: problem sa predefinisanjem operatora!!!31.01.2006. u 01:58 - pre 221 meseci
A pa ne moze i clan i frend:


ili:

Code:

komplex& operator+(const komplex &);
komplex& operator-(const komplex &);
komplex& operator*(const komplex &);
komplex& operator/(const komplex &);


ili:

Code:

friend komplex& operator+(const komplex &,const komplex &);
friend komplex& operator-(const komplex &,const komplex &);
friend komplex& operator*(const komplex &,const komplex &);
friend komplex& operator/(const komplex &,const komplex &);





Preporucio bih ti sledece:

1. u komplex ubaci f-je const double&amp; real(), void real( const double & ), ....

2.ono sto si uradio u operatorima clanova ubaci u friend f-je stim sto ces u njima gde pise real staviti real().
3. imena clanove imag, real promenis u npr _imag, _real ili m_real, m_imag.
4. obrisi operatore clanove sem =.
5. obrisi friend deklaracije jer zbog tacke 1 i 2 to gubi smisao.


trbace ti unarni + i -.

I jos:
referenca u ovakvim slucajevima se ne vraca.
U tvom slucaju treba ovako:
npr za deljenje:
komplex operator/(const komplex &,const komplex &);

...


[Ovu poruku je menjao DDMM dana 31.01.2006. u 03:33 GMT+1]
X
 
Odgovor na temu

[es] :: C/C++ programiranje :: problem sa predefinisanjem operatora!!!

[ Pregleda: 3099 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.