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

gdje je greska ??

[es] :: C/C++ programiranje :: gdje je greska ??

[ Pregleda: 3982 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

refa
Tuzla

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



Profil

icon gdje je greska ??19.12.2005. u 08:17 - pre 223 meseci
Kdevelop c++
nakon kompajliranja ovog kodad javi greske

/root/testni_projekat/src/matrica.h:36: error: `matrica matrica::operator+(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.h:37: error: `matrica matrica::operator-(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.h:38: error: `matrica matrica::operator*(matrica&, matrica&)' must take either zero or one argument
*** Exited with status: 2 ***
ovo gore je nakon kompajliranja projekat.cpp


/root/testni_projekat/src/matrica.h:36: error: `matrica matrica::operator+(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.h:37: error: `matrica matrica::operator-(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.h:38: error: `matrica matrica::operator*(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.cpp:36: error: syntax error before `]' token
/root/testni_projekat/src/matrica.cpp:65: error: `matrica matrica::operator+(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.cpp:80: error: `matrica matrica::operator-(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.cpp:95: error: `matrica matrica::operator*(matrica&, matrica&)' must take either zero or one argument
/root/testni_projekat/src/matrica.cpp:110: error: syntax error at end of input
*** Exited with status: 2 ***
ovo nakon komajliranja matrica.cpp

kode je ispod zasto prijavljuje gresku
i zasto ne mogu da koristim template<class T>
T x;
recim uvijek javi gresku before templets nesto ne znam
btw: mislim da mi destruktor nije dobro implementiran.

matrica.h
#ifndef MATRICA_H
#define MATRICA_H

/**
@author refa
*/
typedef float tip;
class matrica{
private:
int column;
int row;
tip **element;
public:
matrica(int m,int n);
matrica(matrica &x);
matrica& operator=(matrica &x);
matrica operator+(matrica &,matrica &);
matrica operator-(matrica &,matrica &);
matrica operator*(matrica &,matrica &);

~matrica();

};
matrica.cpp
#endif

#include "matrica.h"

matrica::matrica(int m,int n)
{
column=m;
row=n;
element=new tip*[row];
for(int i=0;i<n;++i)
{
element=new tip[column];
}

}
matrica::~matrica()
{
delete []element;
delete element[];
}
matrica::matrica(matrica &x)
{
column=x.column;
row=x.row;
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
element[j]=x.element[j];
}
}
}
matrica& matrica::operator=(matrica &x)
{
column=x.column;
row=x.row;
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
element[j]=x.element[j];
}
}
return *this;
}

matrica matrica::operator +(matrica &x1,matrica &x2)
{
if(x1.column==x2.column && x1.row==x2.row)
{
matrica x(x1);
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
x.element[j]+=x2.element[j];
}
}
return x;
}
}
matrica matrica::operator -(matrica &x1,matrica &x2)
{
if(x1.column==x2.column && x1.row==x2.row)
{
matrica x(x1);
for(int i=0;i<row;++i)
{
for(int j=0;j<column;++j)
{
x.element[j]-=x2.element[j];
}
}
return x;
}
}
matrica matrica::operator *(matrica &x1,matrica &x2)
{
if(x1.row==x2.column)
{
matrica x(x1);
for(int i=0;i<row;++i)
{
tip sum=0;
int j;
for(j=0;j<column;++j)
{
sum+=x.element[j]*x2.element[j];
}
x.element[j]=sum;
}
return x;
}


projekat.cpp
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "matrica.h"
typedef float tip;
#include <iostream>
#include <cstdlib>

using namespace std;


int main(int argc, char *argv[])
{

matrica a(3,4),b(3,4);


return EXIT_SUCCESS;
}



refa
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.180.EUnet.yu.



+9 Profil

icon Re: gdje je greska ??19.12.2005. u 09:47 - pre 223 meseci
Molim te, da bih uopšte pročitao, stavi code tagove.
 
Odgovor na temu

Goran Arandjelovic
Beograd

Član broj: 29116
Poruke: 387
*.180.EUnet.yu.



+9 Profil

icon Re: gdje je greska ??19.12.2005. u 09:55 - pre 223 meseci
Nisam bio ispratio do kraja, ali glavna greška je u tome što stavljaš dva argumenta za operatore. Zašto to? Za ove operatore ti je potreban jedan argument koji će predstavljati referencu na klasu matrica. To nije funkcija kojoj ćeš proslediti dve matrice pa da ona izvrši datu operaciju već prosleđuješ referencu na drugi operand.

Evo ti kako izgleda poziv...

Code:

int main()
{
  matrica x, y, z;
  
  z = x + y;
  /*
    to je isto kao da si napisao z = x.operator+(y)
    ili još preciznije z.operator=(x.operator+(y));
    odnosno, kao što vidiš, operator+ prima samo jedan argument, a ne dva...
}


Nadam se da si razumeo...
Pozdrav.

[Ovu poruku je menjao Goran Arandjelovic dana 19.12.2005. u 10:59 GMT+1]
 
Odgovor na temu

Marko Stankovic

Član broj: 11
Poruke: 306
212.200.120.*



Profil

icon Re: gdje je greska ??19.12.2005. u 13:27 - pre 223 meseci
Da bi ovo radilo kako ti zelis samo stavi da su te metode koje preklapaju operatore prijateljske, odnosno friend i onda mogu da imaju dva argumenta.
Inace kad preklapas binarne operatore ako nisu prijateljske metode onda imaju i skriveni argument this pa zbog toga mozes da prosledis samo jos jedan argument, a ne dva jer bi onda ispalo da su to ternarni operatori.
I drink to make other people interesting.
 
Odgovor na temu

[es] :: C/C++ programiranje :: gdje je greska ??

[ Pregleda: 3982 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

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